// allocate a small chunk of memory. void * kmalloc(size_t size, int gfp_flags) { int i; void * p; for(i=0;i<SIZEOFARRAY(_pools);i++) if( ( gfp_flags & _pools[i].gfp_flags ) == _pools[i].gfp_flags ) if( _pools[i].chunk_size >= size ) { // create the mem_cache if it does not exist. if(!_pools_mem_cache[i]) if( 0 != mem_cache_new( &(_pools_mem_cache[i]), _pools[i].pages, _pools[i].chunk_size, _pools[i].gfp_flags) ) return NULL; // OUT OF MEMORY! // allocate the requested memory. p = mem_cache_alloc( _pools_mem_cache[i] ); if(p && (gfp_flags & GFP_ZERO ) ) memset(p, 0, size); return p; } // bad size or gfp_flags! return NULL; }
bool build_a_list(const char *filename) { size_t len; ssize_t linelen = 0; char *line = NULL; FILE *file = fopen(filename, "r"); if (!file) { LOG(LOG_LEVEL_ERROR, "Open %s failed", filename); return false; } assert(file != NULL); while ((linelen = getline(&line, &len, file)) != -1) { line[linelen-1] = '\0'; // a.list has the same format with w.list and b.list //if (!validate_wb(line, linelen)) // continue; ip_seg ipseg = get_ip_seg_from_mask_str(line); if (ipseg.end == 0) continue; ip_seg* seg = (ip_seg*)mem_cache_alloc(ipseg_cache); *seg = ipseg; list_append(a_list, seg); } if (line) free(line); fclose(file); return true; }
void *mc_realloc(void *ptr, size_t size) { void *new_ptr; mem_cache_ptr mem_cache; mem_cache_header_ptr mem_cache_header; if (NULL == ptr || size <= 0) { return NULL; } mem_cache_header = (mem_cache_header_ptr)(ptr - sizeof(mem_cache_header_t)); mem_cache = mem_cache_header->mem_cache; if (mem_cache_header->size <= size) { return ptr; } if (mem_cache != redis_mem_cache) { return NULL; } if (MEMCACHE_MAGIC_NUMBER != mem_cache->magic_number) { return NULL; } if (mem_cache_header->size <= 0) { return NULL; } new_ptr = mem_cache_alloc(redis_mem_cache, size); if (NULL == new_ptr) { return NULL; } memcpy(new_ptr, ptr, mem_cache_header->size); mem_cache_free(ptr); return new_ptr; }
struct mem_cache_t *mem_cache_create(size_t objsize, uint32_t capacity) { struct mem_cache_t *cachep; void *parray_mem; cachep = mem_cache_alloc(cache_cache); if (cachep == NULL) return NULL; parray_mem = __dma_mem_memalign(L1_CACHE_BYTES, sizeof(void *) * capacity); if (parray_mem == NULL) { mem_cache_free(cache_cache, cachep); return NULL; } cachep->objsize = objsize; cachep->free_limit = capacity; cachep->ptr_stack = parray_mem; cachep->next_free = 0; cachep->obj_allocated = 0; mutex_init(&(cachep->mmlock)); return cachep; }
bool record2map(uint32_t addr, const logrecord_t *record, port_type_t type) { hash_table_t *map = get_map_by_type(type); if (!map) return false; logrecord_t* original_record = (logrecord_t*) hash_table_lookup(map, &addr); if (original_record == NULL) { uint32_t* ip = (uint32_t* )mem_cache_alloc(key_cache); original_record = (logrecord_t* )mem_cache_alloc(value_cache); *original_record = *record; *ip = addr; hash_table_insert(map, ip, original_record); } else { original_record->count += record->count; original_record->bytes += record->bytes; original_record->flag |= record->flag; } return true; }
bool build_wb_list(const char *filename) { size_t len; ssize_t linelen = 0; char *line = NULL; FILE *file = fopen(filename, "r"); if (!file) { LOG(LOG_LEVEL_ERROR, "Open %s failed", filename); return false; } assert(file != NULL); while ((linelen = getline(&line, &len, file)) != -1) { line[linelen-1] = '\0'; //if (!validate_wb(line, linelen)) // continue; ip_seg ipseg = get_ip_seg_from_mask_str(line); if (ipseg.end == 0) continue; for (uint32_t ip = ipseg.start; ip <= ipseg.end; ip++) { uint32_t* key = (uint32_t* )mem_cache_alloc(key_cache); *key = ip; hash_table_insert(wb_list_map, key, NULL); } //LOG(LOG_LEVEL_TRACE, "Read a line: %s, length: %zu", line, linelen); } if (line) free(line); fclose(file); return true; }
item *item_alloc(char *key, int flags, rel_time_t exptime, int nbytes) { int nsuffix, ntotal, len; item *it; unsigned int id; char suffix[40]; ntotal = item_make_header(key, flags, nbytes, suffix, &nsuffix, &len); id = mem_cache_clsid(mem_cache, ntotal); if (id == 0) return 0; it = mem_cache_alloc(mem_cache, ntotal); if (it == 0) { int tries = 50; item *search; /* If requested to not push old items out of cache when memory runs out, * we're out of luck at this point... */ if (!settings.evict_to_free) return 0; /* * try to get one off the right LRU * don't necessariuly unlink the tail because it may be locked: refcount>0 * search up from tail an item with refcount==0 and unlink it; give up after 50 * tries */ if (id > LARGEST_ID) return 0; if (tails[id]==0) return 0; for (search = tails[id]; tries>0 && search; tries--, search=search->prev) { if (search->refcount==0) { item_unlink(search); break; } } it = mem_cache_alloc(mem_cache, ntotal); if (it==0) return 0; } assert(it->slabs_clsid == 0); it->slabs_clsid = id; assert(it != heads[it->slabs_clsid]); it->next = 0; it->prev = 0; it->h_next = 0; it->refcount = 0; it->it_flags = 0; it->nkey = len; it->nbytes = nbytes; strcpy(ITEM_key(it), key); it->exptime = exptime; memcpy(ITEM_suffix(it), suffix, nsuffix); it->nsuffix = nsuffix; return it; }
/* Allocate, clear and return element */ void *mem_cache_zalloc(struct mem_cache *cache) { void *elem = mem_cache_alloc(cache); memset(elem, 0, cache->struct_size); return elem; }
void *mc_alloc(size_t n, size_t size) { void *ptr = mem_cache_alloc(redis_mem_cache, size * n); bzero(ptr, size * n); return ptr; }
void *mc_malloc(size_t size) { return mem_cache_alloc(redis_mem_cache, size); }
void *mem_cache_alloc_wrapped(int size) { return mem_cache_alloc(this); }