static int unregister_ptr(void *ptr) { int i = ptr_position(ptr); if (NULL == malloc_table[i].ptr) { return 0; } malloc_table[i].ptr = NULL; free_size += malloc_table[i].size; for (i = (i + 1) % SZ; NULL != malloc_table[i].ptr; i = (i + 1) % SZ) { const void *ptr2 = malloc_table[i].ptr; int j = hash_pointer(ptr2) % SZ; for (; malloc_table[j].ptr != NULL; j = (j + 1) % SZ) if (ptr2 == malloc_table[i].ptr) goto count_outer; malloc_table[j] = malloc_table[i]; malloc_table[i].ptr = NULL; malloc_table[i].size = 0; count_outer: ; } return 1; }
void run_finalizers(at *q) { unsigned int h = hash_pointer(q) % HASHTABLESIZE; finalizer **fp = & finalizers[h]; finalizer *todo = 0; /* collect */ while (*fp) { finalizer *f = *fp; if (f->target != q) { fp = &f->next; } else { *fp = f->next; f->next = todo; todo = f; } } /* execute */ q->flags &= ~ C_FINALIZER; while (todo) { finalizer *f = todo; todo = f->next; (*f->func)(q, f->arg); deallocate(&finalizer_alloc, (struct empty_alloc*)f); } }
int main(){ //This is a test for smalloc. int * dat=smalloc(20*sizeof(int)); int * dat2=smalloc(20*sizeof(int)); printf("Dat: %p %04x\n",dat,hash_pointer(dat)); printf("Dat2: %p %04x\n",dat2,hash_pointer(dat2)); sfree(dat); sfree(dat); sfree(dat2); dat=smalloc(20*sizeof(int)); dat2=smalloc(20*sizeof(int)); printf("Dat: %p %04x\n",dat,hash_pointer(dat)); printf("Dat2: %p %04x\n",dat2,hash_pointer(dat2)); sfree(dat); sfree(dat2); return 0; }
static unsigned long oostruct_hash(at *p) { unsigned long x = 0x87654321; object_t *obj = Mptr(p); class_t *cl = Class(p); x ^= hash_pointer((void*)cl); for(int i=0; i<cl->num_slots; i++) { x = (x<<1) | ((long)x<0 ? 1 : 0); x ^= hash_value(obj->slots[i]); } return x; }
static inline int ptr_position(const void *ptr) { int i = hash_pointer(ptr) % SZ; for (; NULL != malloc_table[i].ptr; i = (i + 1) % SZ) { if (malloc_table[i].ptr == ptr) { return i; } } return i; }
static void register_first_touch(int pid, void * start_of_page) { size_t hash = hash_pointer(start_of_page, 0); /* Is this the first time we touch this page ? */ ft_entry * t = htable_get(&firsttouch, hash, ptrequ, start_of_page); /* If not record the touching thread to the firsttouch htable */ if (!t) { t = malloc(sizeof(ft_entry)); t->tid = pid; t->start_of_page = start_of_page; htable_add(&firsttouch, hash, t); debug_print("First touch by %d detected at %p\n", pid, start_of_page); } ft_entry * x = htable_get(&firsttouch, hash, ptrequ, start_of_page); }
static struct aga_node *nr_to_n(struct agar_state *sr, const void *nr) { struct agar_node *nn; size_t hash = hash_pointer(nr, HASH_BASE); bool rc; nn = htable_get(&sr->nodes, hash, agar_node_cmp, nr); if (!nn) { nn = tal(sr, struct agar_node); assert(nn); nn->nr = nr; aga_node_init(&nn->n); rc = htable_add(&sr->nodes, hash, nn); assert(rc); }
void add_finalizer(at *q, void (*func)(at*,void*), void *arg) { if (q) { unsigned int h = hash_pointer(q) % HASHTABLESIZE; /* Already there? */ if (q->flags & C_FINALIZER) for(finalizer_t *f = finalizers[h]; f; f=f->next) if (f->target==q && f->func==func && f->arg==arg) return; /* Add a new one */ finalizer_t *f = allocate(&finalizer_alloc_root); f->next = finalizers[h]; f->target = q; f->func = func; f->arg = arg; finalizers[h] = f; q->flags |= C_FINALIZER; } }
mutex_for_pointer(void *p) { OVS_ALIGNED_STRUCT(CACHE_LINE_SIZE, aligned_mutex) { struct ovs_mutex mutex; char pad[PAD_SIZE(sizeof(struct ovs_mutex), CACHE_LINE_SIZE)]; }; static struct aligned_mutex atomic_mutexes[] = { #define MUTEX_INIT { .mutex = OVS_MUTEX_INITIALIZER } #define MUTEX_INIT4 MUTEX_INIT, MUTEX_INIT, MUTEX_INIT, MUTEX_INIT #define MUTEX_INIT16 MUTEX_INIT4, MUTEX_INIT4, MUTEX_INIT4, MUTEX_INIT4 MUTEX_INIT16, MUTEX_INIT16, }; BUILD_ASSERT_DECL(IS_POW2(ARRAY_SIZE(atomic_mutexes))); uint32_t hash = hash_pointer(p, 0); uint32_t indx = hash & (ARRAY_SIZE(atomic_mutexes) - 1); return &atomic_mutexes[indx].mutex; } void atomic_lock__(void *p) OVS_ACQUIRES(mutex_for_pointer(p)) { ovs_mutex_lock(mutex_for_pointer(p)); } void atomic_unlock__(void *p) OVS_RELEASES(mutex_for_pointer(p)) {
static size_t agar_node_hash(const struct agar_node *nn) { return hash_pointer(nn->nr, HASH_BASE); }
static size_t rehash (const void *e, void *unused) { const ft_entry * ft = (const ft_entry *) e; return hash_pointer(ft->start_of_page, 0); }
/* We hash the pointers, which will be identical for same call. */ static unsigned long hash_trace(const struct trace *trace) { return hash_pointer(trace->condstr, hash_pointer(trace->file, trace->line + trace->expect)); }
static inline hashval_t htab_hash (hash_entry_type element) { return hash_pointer (element->addr); }