github.com/llvm-mirror/llgo@v0.0.0-20190322182713-bf6f0a60fce1/third_party/gofrontend/libgo/runtime/go-map-delete.c (about)

     1  /* go-map-delete.c -- delete an entry from a map.
     2  
     3     Copyright 2009 The Go Authors. All rights reserved.
     4     Use of this source code is governed by a BSD-style
     5     license that can be found in the LICENSE file.  */
     6  
     7  #include <stddef.h>
     8  #include <stdlib.h>
     9  
    10  #include "runtime.h"
    11  #include "malloc.h"
    12  #include "go-alloc.h"
    13  #include "go-assert.h"
    14  #include "map.h"
    15  
    16  /* Delete the entry matching KEY from MAP.  */
    17  
    18  void
    19  __go_map_delete (struct __go_map *map, const void *key)
    20  {
    21    const struct __go_map_descriptor *descriptor;
    22    const struct __go_type_descriptor *key_descriptor;
    23    uintptr_t key_offset;
    24    const FuncVal *equalfn;
    25    size_t key_hash;
    26    size_t key_size;
    27    size_t bucket_index;
    28    void **pentry;
    29  
    30    if (map == NULL)
    31      return;
    32  
    33    descriptor = map->__descriptor;
    34  
    35    key_descriptor = descriptor->__map_descriptor->__key_type;
    36    key_offset = descriptor->__key_offset;
    37    key_size = key_descriptor->__size;
    38    if (key_size == 0)
    39      return;
    40  
    41    __go_assert (key_size != -1UL);
    42    equalfn = key_descriptor->__equalfn;
    43  
    44    key_hash = __go_call_hashfn (key_descriptor->__hashfn, key, key_size);
    45    bucket_index = key_hash % map->__bucket_count;
    46  
    47    pentry = map->__buckets + bucket_index;
    48    while (*pentry != NULL)
    49      {
    50        char *entry = (char *) *pentry;
    51        if (__go_call_equalfn (equalfn, key, entry + key_offset, key_size))
    52  	{
    53  	  *pentry = *(void **) entry;
    54  	  if (descriptor->__entry_size >= TinySize)
    55  	    __go_free (entry);
    56  	  map->__element_count -= 1;
    57  	  break;
    58  	}
    59        pentry = (void **) entry;
    60      }
    61  }