コード例 #1
0
ファイル: jgmalloc.c プロジェクト: jamesgolick/jgmalloc
mchunkptr
jgmalloc(size_t size) {
  if (free_list_is_empty()) {
    allocate();
    // if we still don't have any memory we're f****d.
    if (free_list_is_empty()) { return NULL; }
  }

  mchunkptr cur = freeListHead;
  do {
    if (cur->size >= mchunk_aligned_size(size)) {
      free_list_remove(cur);

      if (mchunk_should_split(cur, size)) {
	mchunkptr nxt = mchunk_split(cur, size);
	assert(mchunk_chunk_right(cur) == nxt);
	free_list_append(nxt);

	return cur;
      } else {
	return cur;
      }
    }
  } while((cur = cur->next) != NULL);

  allocate();
  return jgmalloc(size);
}
コード例 #2
0
ファイル: slot_map.c プロジェクト: ElArtista/EnergyCore
static sm_key slot_map_next_key(struct slot_map* sm)
{
    sm_key k;
    /* Allocate new indexes */
    if (sm->cap_slots - sm->num_slots < 1)
        slots_resize(sm, sm->cap_slots * 2);
    assert(sm->free_list_head != INVALID_IDX);
    /* Next index */
    k.index = sm->free_list_head;
    k.generation = sm->slots[k.index].generation;
    free_list_remove(sm, sm->free_list_head);
    sm->num_slots++;
    return k;
}
コード例 #3
0
ファイル: slot_map.c プロジェクト: ElArtista/EnergyCore
void* slot_map_foreign_add(struct slot_map* sm, sm_key k, void* data)
{
    assert(slot_map_key_valid(k));
    if (!(k.index < sm->cap_slots && sm->slots[k.index].data_idx != INVALID_IDX)) {
        if (k.index >= sm->cap_slots)
            slots_resize(sm, k.index + 1);
        size_t ndata_idx = data_append(sm, data);
        free_list_remove(sm, k.index);
        sm->slots[k.index].generation = k.generation;
        sm->slots[k.index].data_idx = ndata_idx;
        sm->data_to_slot[ndata_idx] = k.index;
        sm->num_slots++;
        return slot_map_data(sm, ndata_idx);
    }
    return 0;
}
コード例 #4
0
/*
 * bcm_mp_alloc() - Allocate a memory pool object.
 *
 * Parameters:
 *    pool:    INPUT    The handle to the pool.
 *
 * Returns:
 *    A pointer to the new object. NULL on error.
 *
 */
void* bcm_mp_alloc(bcm_mp_pool_h pool)
{
	bcm_mp_free_list_t *obj_hdr;
	void               *objp = NULL;

	/* Check parameters */
	ASSERT(pool != NULL);


	if (BCM_MP_GET_POOL_TYPE(pool) == BCM_MP_TYPE_PREALLOC) {
		if (pool->u.p.free_objp) {
			/* Allocate memory object from pool. */
			obj_hdr = free_list_remove(&pool->u.p.free_objp);

			/* Clear new object header */
			memset(obj_hdr, 0, sizeof(*obj_hdr));
			objp = obj_hdr;
		}
	}
	else {
		/* Heap allocation memory pool. */
		objp = MALLOC(pool->u.h.osh, pool->objsz);
	}

	/* Update pool state. */
	if (objp) {
		pool->num_alloc++;
		if (pool->num_alloc > pool->high_water) {
			pool->high_water = pool->num_alloc;
		}
	} else {
		pool->failed_alloc++;
	}

	return (objp);
}
コード例 #5
0
ファイル: hashmap.cpp プロジェクト: tangwsh/Common
THashNode* CHashMap::insert_node(void * key, void* new_data, int new_len)
{
    THashNode* node = free_list_remove();
    if (node == NULL) 
    {
        return NULL;                
    }
    
    int new_chunk_num = allocator_.get_chunk_num(new_len);
    BC_MEM_HANDLER head_hdr = allocator_.malloc(new_chunk_num);
    if(head_hdr == INVALID_BC_MEM_HANDLER)
    {
        free_list_insert(node);
        return NULL;
    }
    allocator_.split(head_hdr, new_data, new_len);
    use_node(node, key, new_len, head_hdr);

	//将该节点插入到附加链表头部
	node->add_info_1_ = time(NULL);
	++node->add_info_2_;
	insert_add_list_head(node);
	return node;
}
コード例 #6
0
/*
 * create_pool() - Create a new pool for fixed size objects. Helper function
 *                         common to all memory pool types.
 *
 * Parameters:
 *    mgr:           INPUT  The handle to the pool manager
 *    obj_sz:        INPUT  Size of objects that will be allocated by the new pool.
 *                          This is the size requested by the user. The actual size
 *                          of the memory object may be padded.
 *                          Must be > 0 for heap pools. Must be >= sizeof(void *) for
 *                          Prealloc pools.
 *    padded_obj_sz: INPUT  Size of objects that will be allocated by the new pool.
 *                          This is the actual size of memory objects. It is 'obj_sz'
 *                          plus optional padding required for alignment.
 *                          Must be > 0 for heap pools. Must be >= sizeof(void *) for
 *                          Prealloc pools.
 *    nobj:          INPUT  Maximum number of concurrently existing objects to support.
 *                          Must be specified for Prealloc pool. Ignored for heap pools.
 *    memstart       INPUT  Pointer to the memory to use, or NULL to malloc().
 *                          Ignored for heap pools.
 *    memsize        INPUT  Number of bytes referenced from memstart (for error checking).
 *                          Must be 0 if 'memstart' is NULL. Ignored for heap pools.
 *    poolname       INPUT  For instrumentation, the name of the pool
 *    type           INPUT  Pool type - BCM_MP_TYPE_xxx.
 *    newp:          OUTPUT The handle for the new pool, if creation is successful
 *
 * Returns:
 *    BCME_OK   Pool created ok.
 *    other     Pool not created due to indicated error. newpoolp set to NULL.
 *
 *
 */
static int BCMATTACHFN(create_pool)(bcm_mpm_mgr_h mgr,
                                    unsigned int obj_sz,
                                    unsigned int padded_obj_sz,
                                    int nobj,
                                    void *memstart,
                                    char poolname[BCM_MP_NAMELEN],
                                    uint16 type,
                                    bcm_mp_pool_h *newp)
{
	bcm_mp_pool_t      *mem_pool = NULL;
	void               *malloc_memstart = NULL;

	/* Check parameters */
	if ((mgr == NULL) ||
	    (newp == NULL) ||
	    (obj_sz == 0) ||
	    (padded_obj_sz == 0) ||
	    (poolname == NULL) ||
	    (poolname[0] == '\0')) {
		return (BCME_BADARG);
	}

	/* Allocate memory object from pool. */
	mem_pool = (bcm_mp_pool_t *) free_list_remove(&mgr->free_pools);
	if (mem_pool == NULL) {
		return (BCME_NOMEM);
	}

	/* For pool manager allocated memory, malloc the contiguous memory
	 * block of objects.
	 */
	if ((type == BCM_MP_TYPE_PREALLOC) && (memstart == NULL)) {
		memstart = MALLOC(mgr->osh, padded_obj_sz * nobj);
		if (memstart == NULL) {
			free_list_add(&mgr->free_pools,
			                     (bcm_mp_free_list_t *) mem_pool);
			return (BCME_NOMEM);
		}
		malloc_memstart = memstart;
	}

	/* Init memory pool object (common). */
	memset(mem_pool, 0, sizeof(*mem_pool));
	mem_pool->objsz = obj_sz;
	BCM_MP_SET_POOL_TYPE(mem_pool, type);
	BCM_MP_SET_IN_USE(mem_pool);
	strncpy(mem_pool->name, poolname, sizeof(mem_pool->name));
	mem_pool->name[sizeof(mem_pool->name)-1] = '\0';


	if (type == BCM_MP_TYPE_PREALLOC) {
		/* Init memory pool object (Prealloc specific). */
		mem_pool->u.p.nobj            = (uint16) nobj;
		mem_pool->u.p.malloc_memstart = malloc_memstart;
		mem_pool->u.p.padded_objsz    = padded_obj_sz;

		/* Chain all the memory objects onto the pool's free list. */
		free_list_init(&mem_pool->u.p.free_objp, memstart, padded_obj_sz, nobj);
	}
	else {
		/* Init memory pool object (Heap specific). */
		mem_pool->u.h.osh = mgr->osh;
	}

	mgr->npools++;
	*newp = mem_pool;
	return (BCME_OK);
}