void operator()( int id ) const {
        const int ITERS = 1000;
        void *local[ITERS];

        startB.wait();
        for (int i=id*OBJ_CNT; i<(id+1)*OBJ_CNT; i++) {
            afterTerm[i] = pool_malloc(pool, i%2? 8*1024 : 9*1024);
            memset(afterTerm[i], i, i%2? 8*1024 : 9*1024);
            crossThread[i] = pool_malloc(pool, i%2? 9*1024 : 8*1024);
            memset(crossThread[i], i, i%2? 9*1024 : 8*1024);
        }

        for (int i=1; i<ITERS; i+=2) {
            local[i-1] = pool_malloc(pool, 6*1024);
            memset(local[i-1], i, 6*1024);
            local[i] = pool_malloc(pool, 16*1024);
            memset(local[i], i, 16*1024);
        }
        mallocDone.wait();
        int myVictim = threadNum-id-1;
        for (int i=myVictim*OBJ_CNT; i<(myVictim+1)*OBJ_CNT; i++)
            pool_free(pool, crossThread[i]);
        for (int i=0; i<ITERS; i++)
            pool_free(pool, local[i]);
    }
void TestFixedBufferPool()
{
    void *ptrs[7];
    rml::MemPoolPolicy pol(fixedBufGetMem, NULL, 0, /*fixedSizePool=*/true,
                           /*keepMemTillDestroy=*/false);
    rml::MemoryPool *pool;

    pool_create_v1(0, &pol, &pool);
    void *largeObj = pool_malloc(pool, 7*1024*1024);
    ASSERT(largeObj, NULL);
    pool_free(pool, largeObj);

    for (int i=0; i<7; i++) {
        ptrs[i] = pool_malloc(pool, 1024*1024);
        ASSERT(ptrs[i], NULL);
    }
    for (int i=0; i<7; i++)
        pool_free(pool, ptrs[i]);

    largeObj = pool_malloc(pool, 7*1024*1024);
    ASSERT(largeObj, NULL);
    pool_free(pool, largeObj);

    pool_destroy(pool);
}
示例#3
0
文件: proxy.c 项目: Nadrin/mcproxy
static int
helper_slot(cid_t client_id, char mode, unsigned char msg_id,
            nethost_t* host, objlist_t* data, void* extra)
{
  objlist_t* slot;
  slot_t slotdata;
  
  size_t index = (size_t)extra;

  if(mode == MODE_RECV) {
    slot = pool_malloc(NULL, sizeof(objlist_t));
    
    slot->objects = pool_malloc(NULL, 4 * sizeof(object_t));
    slot->count   = 4;
    
    if(proto_recv_slot(host, 0, slot) != 0)
      return PROXY_ERROR;
    data->objects[index].data = (void*)slot;
  }
  else {
    slot = proto_list(data, index);
    if(proto_send_slot(host, 0, slot) != 0)
      return PROXY_ERROR;
  }

  proto_getslot(slot, 0, &slotdata);
  if(slotdata.datasize > 0)
    return proxy_transfer(mode, host, slot, slotdata.datasize);
  return PROXY_OK;
}
    void operator()( int id ) const {
        rml::MemPoolPolicy pol(CrossThreadGetMem, CrossThreadPutMem);
        const int objLen = 10*id;

        pool_create_v1(id, &pol, &pool[id]);
        obj[id] = (char*)pool_malloc(pool[id], objLen);
        ASSERT(obj[id], NULL);
        memset(obj[id], id, objLen);

        {
            const size_t lrgSz = 2*16*1024;
            void *ptrLarge = pool_malloc(pool[id], lrgSz);
            ASSERT(ptrLarge, NULL);
            memset(ptrLarge, 1, lrgSz);

            // consume all small objects
            while (pool_malloc(pool[id], 5*1024))
                ;
            // releasing of large object can give a chance to allocate more
            pool_free(pool[id], ptrLarge);

            ASSERT(pool_malloc(pool[id], 5*1024), NULL);
        }

        barrier.wait();
        int myPool = number_of_threads-id-1;
        for (int i=0; i<10*myPool; i++)
            ASSERT(myPool==obj[myPool][i], NULL);
        pool_free(pool[myPool], obj[myPool]);
        pool_destroy(pool[myPool]);
    }
/* test that pools in small space are either usable or not created
   (i.e., exception raised) */
void TestSmallFixedSizePool()
{
    char *buf;
    bool allocated = false;

    for (size_t sz = 0; sz < 64*1024; sz = sz? 3*sz : 3) {
        buf = (char*)malloc(sz);
#if TBB_USE_EXCEPTIONS
        try {
            tbb::fixed_pool pool(buf, sz);
/* Check that pool is usable, i.e. such an allocation exists,
   that can be fulfilled from the pool. 16B allocation fits in 16KB slabs,
   so it requires at least 16KB. Requirement of 9KB allocation is more modest.
*/
            allocated = pool.malloc( 16 ) || pool.malloc( 9*1024 );
            ASSERT(allocated, "If pool created, it must be useful.");
        } catch (std::bad_alloc) {
        } catch (...) {
            ASSERT(0, "wrong exception type; expected bad_alloc");
        }
#else
/* Do not test high-level pool interface because pool ctor emit exception
   on creation failure. Instead test same functionality via low-level interface.
   TODO: add support for configuration with disabled exceptions to pools.
*/
        rml::MemPoolPolicy pol(fixedBufGetMem, NULL, 0, /*fixedSizePool=*/true,
                               /*keepMemTillDestroy=*/false);
        rml::MemoryPool *pool;
        FixedPool fixedPool(buf, sz);

        rml::MemPoolError ret = pool_create_v1((intptr_t)&fixedPool, &pol, &pool);

        if (ret == rml::POOL_OK) {
            allocated = pool_malloc(pool, 16) || pool_malloc(pool, 9*1024);
            ASSERT(allocated, "If pool created, it must be useful.");
            pool_destroy(pool);
        } else
            ASSERT(ret == rml::NO_MEMORY, "Expected that pool either valid "
                                     "or have no memory to be created");
#endif
        free(buf);
    }
    ASSERT(allocated, "Maximal buf size should be enough to create working fixed_pool");
#if TBB_USE_EXCEPTIONS
    try {
        tbb::fixed_pool pool(NULL, 10*1024*1024);
        ASSERT(0, "Useless allocator with no memory must not be created");
    } catch (std::bad_alloc) {
    } catch (...) {
        ASSERT(0, "wrong exception type; expected bad_alloc");
    }
#endif
}
NSAPI_PUBLIC PList_t
PListCreate(pool_handle_t *mempool, int resvprop, int maxprop, int flags)
{
    PListStruct_t *plist;       /* pointer to property list structure */
    int i;

    plist = (PListStruct_t *)pool_malloc(mempool, sizeof(PListStruct_t));
    if (plist) {

        /* Negative maxprop is the same as zero, i.e. no limit */
        if (maxprop < 0) maxprop = 0;

        /* If resvprop and maxprop are both specified, limit resvprop */
        if (resvprop > 0) {
            if (maxprop && (resvprop > maxprop)) resvprop = maxprop;
        }
        else resvprop = 0;

        /* Initialize property list structure */
        plist->pl_mempool = mempool;
        plist->pl_symtab = NULL;
        plist->pl_maxprop = maxprop;
        plist->pl_resvpi = resvprop;
        plist->pl_initpi = resvprop;
        plist->pl_lastpi = resvprop;

        /* Set initialize size for array of property value pointers */
        plist->pl_cursize = (resvprop) ? resvprop : PLIST_DEFSIZE;

        /* Allocate the initial array of property value pointers */
        plist->pl_ppval = (pb_entry **)pool_malloc(mempool,
                                   	           (plist->pl_cursize *
                                                    sizeof(PLValueStruct_t *)));
        if (!plist->pl_ppval) {

            /* Failed - insufficient memory */
            pool_free(mempool, (void *)plist);
            plist = NULL;
        }
        else {
            /* NULL out pointers in the reserved index range, if any */
            for (i = 0; i < plist->pl_lastpi; ++i) {
                plist->pl_ppval[i] = 0;
            }
        }
    }

    return (PList_t)plist;
}
// single pool shared by different threads
void TestSharedPool()
{
    rml::MemPoolPolicy pol(getMallocMem, putMallocMem);
    rml::MemoryPool *pool;

    pool_create_v1(0, &pol, &pool);
    void **crossThread = new void*[MaxThread * SharedPoolRun::OBJ_CNT];
    void **afterTerm = new void*[MaxThread * SharedPoolRun::OBJ_CNT];

    for (int p=MinThread; p<=MaxThread; p++) {
        SharedPoolRun::init(p, pool, crossThread, afterTerm);
        SharedPoolRun thr;

        void *hugeObj = pool_malloc(pool, 10*1024*1024);
        ASSERT(hugeObj, NULL);

        NativeParallelFor( p, thr );

        pool_free(pool, hugeObj);
        for (int i=0; i<p*SharedPoolRun::OBJ_CNT; i++)
            pool_free(pool, afterTerm[i]);
    }
    delete []afterTerm;
    delete []crossThread;

    pool_destroy(pool);
    ASSERT(!liveRegions, "Expected all regions were released.");
}
示例#8
0
void *malloc (size_t size)
{
    void *ret;
    int pool_idx;
    
    if (malloc_base == NULL || size == 0) {
        ret = NULL;
    } else if (size >= MAX_ELEM_SIZE) {
        ret = big_malloc((size + PAGE_SIZE - 1) / PAGE_SIZE);
    } else {
        if (size <= MIN_ELEM_SIZE)
            pool_idx = 0;
        else {
            pool_idx = get_pool_idx(size);
        }
        ret = pool_malloc(pool_idx);
    }
    if (ret != NULL)
        memset(ret, 0, size);
#if 0
    memory_dump();
    printf("%s(%d) => %p\n", __func__, size, ret);
#endif

    return ret;
}
示例#9
0
文件: pool.c 项目: noelbk/bklib
// does pool_free(*dst), then *dst = malloc(len)
void*
pool_copy_buf(pool_t *pool, void **dst, const void *buf, size_t len) {
    pool_free(pool, *dst);
    *dst = pool_malloc(pool, len);
    memcpy(*dst, buf, len);
    return *dst;
}
示例#10
0
void
http_redirect_select_accept(fdselect_t *sel, fd_t fd, int state, void *arg) {
    http_redirect_t *server = (http_redirect_t *)arg;
    http_redirect_client_t *client;
    struct sockaddr_in addr;
    sockaddrlen_t addrlen;
    sock_t sock;
    int i, err=-1;
    char buf1[1024];

    do {
	addrlen = sizeof(addr);
	sock = accept(server->accept_sock, (struct sockaddr*)&addr, &addrlen);
	assertb(sock>=0 || sock_wouldblock(server->accept_sock, sock));
	
	debug(DEBUG_INFO,
	      ("http_redirect_select_accept: accepted from %s\n"
	       ,iaddr_fmt(&addr, buf1, sizeof(buf1)))
	      );

	client = pool_malloc(server->pool, sizeof(*client));
	assertb(client);
	
	i = http_redirect_client_init(server, client);
	assertb(i==0);
	client->sock = sock;
	client->addr = addr;
	i = fdselect_set(server->fdselect, client->sock, FDSELECT_READ, 
			 http_redirect_client_select_read, client);
	assertb(i>=0);
	err = 0;
    } while(0);
}
示例#11
0
static
bool_t
prf_vertex_with_color_load_f(
    prf_node_t * node,
    prf_state_t * state,
    bfile_t * bfile )
{
    uint32_t pos = 4;

    assert( node != NULL && state != NULL && bfile != NULL );

    node->opcode = bf_get_uint16_be( bfile );
    if ( node->opcode != prf_vertex_with_color_info.opcode ) {
        prf_error( 9, "tried vertex with color load method for node of type %d.",
            node->opcode );
        bf_rewind( bfile, 2 );
        return FALSE;
    }

    node->length = bf_get_uint16_be( bfile );
    if ( node->length < 32 ) {
        prf_error( 6, "vertex (%d) so short (%d bytes) it is not supported.",
            node->opcode, node->length );
        bf_rewind( bfile, 4 );
        return FALSE;
    }

    if ( node->length > 4 && node->data == NULL ) { /* not preallocated */
        assert( state->model != NULL );
        if ( state->model->mempool_id == 0 )
            node->data = (uint8_t *)malloc( node->length + NODE_DATA_PAD );
        else
            node->data = (uint8_t *)pool_malloc( state->model->mempool_id,
                node->length + NODE_DATA_PAD );
        if ( node->data == NULL ) {
            prf_error( 9, "memory allocation problem (returned NULL)" );
            bf_rewind( bfile, 8 );
            return FALSE;
        }
    }

    do {
        node_data * data = (node_data *) node->data;
        data->color_name_index = bf_get_uint16_be( bfile ); pos += 2;
        data->flags = bf_get_uint16_be( bfile ); pos += 2;
        prf_dblwrite( data->x, bf_get_float64_be( bfile )); pos += 8;
        prf_dblwrite( data->y, bf_get_float64_be( bfile )); pos += 8;
        prf_dblwrite( data->z, bf_get_float64_be( bfile )); pos += 8;
        if ( node->length < (pos + 4) ) break;
        data->packed_color = bf_get_uint32_be( bfile ); pos += 4;
        if ( node->length < (pos + 4) ) break;
        data->color_index = bf_get_uint32_be( bfile ); pos += 4;
    } while ( FALSE );

    if ( pos < node->length ) /* padding */
        pos += bf_read( bfile, node->data + pos - 4 + NODE_DATA_PAD,
            node->length - pos );

    return TRUE;
} /* prf_vertex_with_color_load_f() */
示例#12
0
static void *tbb_pool_malloc(struct memkind *kind, size_t size)
{
    if(size_out_of_bounds(size)) return NULL;
    void *result = pool_malloc(kind->priv, size);
    if (!result)
        errno = ENOMEM;
    return result;
}
示例#13
0
/* node_new SKIPLIST KEY KLEN VALUE HEIGHT
 * Allocate a new node of the given HEIGHT for SKIPLIST, filling it with the
 * given KLEN-byte KEY (which may be either MINUSINF or PLUSINF, in which case
 * KLEN is ignored) and the given VALUE. */
static struct skiplist_node *node_new(skiplist S, const void *key, const size_t klen, const void *val, size_t height) {
    struct skiplist_node *N, nz = {0};
    N = pool_malloc(S->P, sizeof *N);
    *N = nz;
    N->more = pool_malloc(S->P, (1 + height) * sizeof *N->more);
    if (key != MINUSINF && key != PLUSINF) {
        void *k;
        k = pool_malloc(S->P, klen);
        memcpy(k, key, klen);
        N->k = k;
        N->klen = klen;
    } else
        N->k = (void*)key;
    N->v = (void*)val;
    N->height = height;
    return N;
}
 void operator()(int) const {
     for (size_t sz=minLargeObjectSize; sz<1*1024*1024;
          sz+=LargeObjectCache::largeBlockCacheStep) {
         void *ptr = pool_malloc(mallocPool, sz);
         ASSERT(ptr, "Memory was not allocated");
         memset(ptr, sz, sz);
         pool_free(mallocPool, ptr);
     }
 }
示例#15
0
文件: proxy.c 项目: Nadrin/mcproxy
static int
helper_slot_array(cid_t client_id, char mode, unsigned char msg_id,
                  nethost_t* host, objlist_t* data, void* extra)
{
  objlist_t *array;
  slot_t slotdata;
  int errcode;
  
  size_t i, index = (size_t)extra;
  size_t count    = (size_t)proto_gets(data, index-1);

  if(mode == MODE_RECV) {
    array = pool_malloc(NULL, count * sizeof(objlist_t));
    for(i=0; i<count; i++) {
      array[i].objects = pool_malloc(NULL, 4 * sizeof(object_t));
      array[i].count   = 4;

      if(proto_recv_slot(host, 0, &array[i]) != 0)
        return PROXY_ERROR;

      proto_getslot(&array[i], 0, &slotdata);
      if(slotdata.datasize > 0) {
        errcode = proxy_transfer(mode, host, &array[i], slotdata.datasize);
        if(errcode != PROXY_OK) return errcode;
      }
    }
    data->objects[index].data = (void*)array;
  }
  else {
    array = proto_list(data, index);
    for(i=0; i<count; i++) {
      if(proto_send_slot(host, 0, &array[i]) != 0)
        return PROXY_ERROR;

      proto_getslot(&array[i], 0, &slotdata);
      if(slotdata.datasize > 0) {
        errcode = proxy_transfer(mode, host, &array[i], slotdata.datasize);
        if(errcode != PROXY_OK) return errcode;
      }
    }
  }

  return PROXY_OK;
}
static void TestPoolKeepTillDestroy()
{
    const int ITERS = 50*1024;
    void *ptrs[2*ITERS+1];
    rml::MemPoolPolicy pol(getMemPolicy, putMemPolicy);
    rml::MemoryPool *pool;

    // 1st create default pool that returns memory back to callback,
    // then use keepMemTillDestroy policy
    for (int keep=0; keep<2; keep++) {
        getMemCalls = putMemCalls = 0;
        if (keep)
            pol.keepAllMemory = 1;
        pool_create_v1(0, &pol, &pool);
        for (int i=0; i<2*ITERS; i+=2) {
            ptrs[i] = pool_malloc(pool, 7*1024);
            ptrs[i+1] = pool_malloc(pool, 10*1024);
        }
        ptrs[2*ITERS] = pool_malloc(pool, 8*1024*1024);
        ASSERT(!putMemCalls, NULL);
        for (int i=0; i<2*ITERS; i++)
            pool_free(pool, ptrs[i]);
        pool_free(pool, ptrs[2*ITERS]);
        size_t totalPutMemCalls = putMemCalls;
        if (keep)
            ASSERT(!putMemCalls, NULL);
        else {
            ASSERT(putMemCalls, NULL);
            putMemCalls = 0;
        }
        size_t currGetCalls = getMemCalls;
        pool_malloc(pool, 8*1024*1024);
        if (keep)
            ASSERT(currGetCalls == getMemCalls, "Must not lead to new getMem call");
        size_t currPuts = putMemCalls;
        pool_reset(pool);
        ASSERT(currPuts == putMemCalls, "Pool is not releasing memory during reset.");
        pool_destroy(pool);
        ASSERT(putMemCalls, NULL);
        totalPutMemCalls += putMemCalls;
        ASSERT(getMemCalls == totalPutMemCalls, "Memory leak detected.");
    }

}
static PList_t
PListCreateDuplicate(PList_t src_plist, pool_handle_t *new_mempool, int flags)
{
    PListStruct_t *plist;       /* pointer to property list structure */
    int i;
    pool_handle_t *mempool;

    mempool = (flags == PLFLG_NEW_MPOOL) ? new_mempool : src_plist->pl_mempool;

    plist = (PListStruct_t *)pool_malloc(mempool, sizeof(PListStruct_t));
    if (plist) {

        /* Initialize property list structure */
        plist->pl_mempool = mempool;
        plist->pl_symtab = NULL;
        plist->pl_maxprop = src_plist->pl_maxprop;
        plist->pl_resvpi = src_plist->pl_resvpi;
        plist->pl_initpi = src_plist->pl_initpi;
        plist->pl_lastpi = src_plist->pl_lastpi;

        /* Set initialize size for array of property value pointers */
        plist->pl_cursize = src_plist->pl_cursize; 

        /* Allocate the initial array of property value pointers */
        plist->pl_ppval = (pb_entry **)pool_malloc(mempool,
                                                   (plist->pl_cursize *
                                                    sizeof(PLValueStruct_t *)));
        if (!plist->pl_ppval) {

            /* Failed - insufficient memory */
            pool_free(mempool, (void *)plist);
            plist = NULL;
        }
        else {
            /* NULL out pointers in the reserved index range, if any */
            for (i = 0; i < plist->pl_lastpi; ++i) {
                plist->pl_ppval[i] = 0;
            }
        }
    }

    return (PList_t)plist;
}
示例#18
0
/* skiplist_insert_copy SKIPLIST KEY KLEN VALUE VLEN
 * Make a copy of VLEN bytes of VALUE data in SKIPLIST's private pool, and
 * insert a pointer to the copy under the given KLEN-byte KEY. */
int skiplist_insert_copy(skiplist S, const void *key, const size_t klen, const void *val, const size_t vlen) {
    void *v;
    v = pool_malloc(S->P, vlen);
    memcpy(v, val, vlen);
    if (!skiplist_insert(S, key, klen, v)) {
        pool_free(S->P, v); /* XXX */
        return 0;
    } else
        return 1;
}
示例#19
0
void m_thread() {
    dvdrp_filter_list *pred;
    int8_t ret;
    dvdrp_constraint_list *clist_ptr;
    printf("Hello from DV/DRP test recv!\n");

    net_init();
    dvdrp_init();

    //turn down the power to make it easy to test multihop
    com_ioctl(IFACE_RADIO, CC1000_TX_POWER, 1);

    
    ret = net_proto_set(DVDRP_PROTO_ID);
    if(ret == NET_PROTO_INVALID) {
	printf("Invalid proto\n");
    }

    net_event_register(1, recv);

    pred = pool_malloc(&filter_pool);
    pred->next = NULL;

    clist_ptr = pool_malloc(&constraint_pool);
    clist_ptr->name = TEMP_VALUE;
    clist_ptr->value = 0;
    clist_ptr->compare_type = DVDRP_COMPARE_GT;
    pred->constraints = clist_ptr;

    clist_ptr = pool_malloc(&constraint_pool);
    clist_ptr->name = LIGHT_VALUE;
    clist_ptr->value = 150;
    clist_ptr->compare_type = DVDRP_COMPARE_GT;
    clist_ptr->next = NULL;

    pred->constraints->next = clist_ptr;

    net_ioctl(DVDRP_PROTO_ID, DVDRP_IOCTL_SETPRED, pred, 0);

    mos_led_toggle(2);
    
    printf("Waiting for mesgs.\n\n");
}
示例#20
0
static void node_memverify(xml_data_node *node)
{
	xml_attribute_node *attr_node;
	const char *s1;
	const char *s2;
	const char *s3;
	int region;
	void *new_buffer;
	mess_pile pile;

	/* <memverify> - verifies that a range of memory contains specific data */
	attr_node = xml_get_attribute(node, "start");
	s1 = attr_node ? attr_node->value : NULL;
	if (!s1)
	{
		error_missingattribute("start");
		return;
	}

	attr_node = xml_get_attribute(node, "end");
	s2 = attr_node ? attr_node->value : NULL;
	if (!s2)
		s2 = "0";

	attr_node = xml_get_attribute(node, "region");
	s3 = attr_node ? attr_node->value : NULL;

	memset(&new_command, 0, sizeof(new_command));
	new_command.command_type = MESSTEST_COMMAND_VERIFY_MEMORY;
	new_command.u.verify_args.start = parse_offset(s1);
	new_command.u.verify_args.end = parse_offset(s2);

	if (s3)
	{
		region = memory_region_from_string(s3);
		if (region == REGION_INVALID)
			error_invalidmemregion(s3);
		new_command.u.verify_args.mem_region = region;
	}

	pile_init(&pile);
	messtest_get_data(node, &pile);
	new_buffer = pool_malloc(&command_pool, pile_size(&pile));
	memcpy(new_buffer, pile_getptr(&pile), pile_size(&pile));
	new_command.u.verify_args.verify_data = new_buffer;
	new_command.u.verify_args.verify_data_size = pile_size(&pile);
	pile_delete(&pile);

	if (!append_command())
	{
		error_outofmemory();
		return;
	}
}
示例#21
0
char *pool_strdup_len(memory_pool *pool, const char *src, size_t len)
{
	char *dst = NULL;
	if (src)
	{
		dst = pool_malloc(pool, len + 1);
		if (dst)
			strncpyz(dst, src, len + 1);
	}
	return dst;
}
示例#22
0
int test_memory_pools(void)
{
	memory_pool *pool;
	void *ptrs[16];
	int i;

	has_memory_error = FALSE;
	pool = pool_create(memory_error);
	memset(ptrs, 0, sizeof(ptrs));

	ptrs[0] = pool_malloc(pool, 50);
	ptrs[1] = pool_malloc(pool, 100);

	ptrs[0] = pool_realloc(pool, ptrs[0], 150);
	ptrs[1] = pool_realloc(pool, ptrs[1], 200);

	ptrs[2] = pool_malloc(pool, 250);
	ptrs[3] = pool_malloc(pool, 300);

	ptrs[0] = pool_realloc(pool, ptrs[0], 350);
	ptrs[1] = pool_realloc(pool, ptrs[1], 400);

	ptrs[2] = pool_realloc(pool, ptrs[2], 450);
	ptrs[3] = pool_realloc(pool, ptrs[3], 500);

	ptrs[0] = pool_realloc(pool, ptrs[0], 0);
	ptrs[1] = pool_realloc(pool, ptrs[1], 0);

	ptrs[2] = pool_realloc(pool, ptrs[2], 550);
	ptrs[3] = pool_realloc(pool, ptrs[3], 600);

	/* some heavier stress tests */
	for (i = 0; i < 512; i++)
	{
		ptrs[i % ARRAY_LENGTH(ptrs)] = pool_realloc(pool,
			ptrs[i % ARRAY_LENGTH(ptrs)], rand() % 1000);
	}

	pool_free(pool);
	return has_memory_error;
}
void TestPoolReset()
{
    rml::MemPoolPolicy pol(getMallocMem, putMallocMem);
    rml::MemoryPool *pool;

    pool_create_v1(0, &pol, &pool);
    for (int i=0; i<100; i++) {
        ASSERT(pool_malloc(pool, 8), NULL);
        ASSERT(pool_malloc(pool, 50*1024), NULL);
    }
    int regionsBeforeReset = liveRegions;
    pool_reset(pool);
    for (int i=0; i<100; i++) {
        ASSERT(pool_malloc(pool, 8), NULL);
        ASSERT(pool_malloc(pool, 50*1024), NULL);
    }
    ASSERT(regionsBeforeReset == liveRegions,
           "Expected no new regions allocation.");
    pool_destroy(pool);
    ASSERT(!liveRegions, "Expected all regions were released.");
}
示例#24
0
static void dvdrp_route_copy_pkt_pred(dvdrp_route *route,
				      dvdrp_advert_pkt *pkt)
{
    dvdrp_filter_list     **f_list;
    dvdrp_filter           *cur_filter;
    dvdrp_constraint_list **c_list;
    uint8_t                 i, j, size;

    f_list = &route->pred;
    cur_filter = &(pkt->pred.filters[0]);
//    printf("\np %C\n", pkt->pred.num_filters);
    for(i = 0; i < pkt->pred.num_filters; ++i) {
	*f_list = pool_malloc(&filter_pool);
	size = sizeof(uint8_t);
	c_list = &((*f_list)->constraints);

//	printf(" f %C (%d)\n", cur_filter->num_constraints,
//	       (void*)cur_filter - (void*)pkt);
 	for(j = 0; j < cur_filter->num_constraints; ++j) {
	    *c_list = pool_malloc(&constraint_pool);
	    (*c_list)->name = cur_filter->constraints[j].name;
	    (*c_list)->value = cur_filter->constraints[j].value;
	    (*c_list)->compare_type = cur_filter->constraints[j].compare_type;

//	    printf("   c %C %C %C\n", cur_filter->constraints[j].name,
//		   cur_filter->constraints[j].value,
//		   cur_filter->constraints[j].compare_type);
	    
	    c_list = &((*c_list)->next);
	    size += sizeof(dvdrp_constraint);
	}

	*c_list = NULL;
	cur_filter += size;
	f_list = &(*f_list)->next;
    }
//    printf("\\p\n");
    
    *f_list = NULL;
}
示例#25
0
void *realloc (void *area, size_t new_size)
{
    void *new_area;
    page_descr_t *pool_descr, *cache_descr;
    size_t size;
    int pool_idx, new_pool_idx;

    if (malloc_base == NULL || new_size == 0) {
        free(area);
        return NULL;
    }
    if (area == NULL)
        return malloc(new_size);
    cache_descr = page_cache_get_descr(PAGE(area));
    if (cache_descr == NULL) {
        /* Given area is not valid */
        return NULL;
    }
    pool_idx = page_cache_pool_idx(cache_descr);
    if (new_size >= MAX_ELEM_SIZE) {
        new_pool_idx = POOL_MAX;
        if (pool_idx == POOL_MAX)
            return big_realloc(cache_descr, new_size);
    } else {
        if (new_size <= MIN_ELEM_SIZE)
            new_pool_idx = 0;
        else
            new_pool_idx = get_pool_idx(size);
        if (pool_idx == new_pool_idx)
            return area;
    }
    /* Common case: alloc, copy & free */
    if (new_pool_idx == POOL_MAX)
        new_area = big_malloc((new_size + PAGE_SIZE - 1) / PAGE_SIZE);
    else
        new_area = pool_malloc(new_pool_idx);
    if (new_area == NULL)
        return NULL;
    if (pool_idx == POOL_MAX) {
        pool_descr = page_cache_page_descr(cache_descr);
        size = pool_descr->nb * PAGE_SIZE;
    } else {
        size = MIN_ELEM_SIZE << pool_idx;
    }
    memcpy(new_area, area, size);
    if (pool_idx == POOL_MAX)
        big_free(cache_descr);
    else
        pool_free(cache_descr, area);
    
    return new_area;
}
const Result *result_integer(Session *sn, Request *rq, PRInt64 i)
{
    pool_handle_t *pool = request_pool(rq);

    Result *result = (Result *) pool_malloc(pool, sizeof(Result));
    if (result == NULL)
        return &Result::out_of_memory;

    Context context(sn, rq, pool);
    *result = context.createIntegerResult(i);

    return result;
}
const Result *result_expr(Session *sn, Request *rq, const Expression *expr)
{
    pool_handle_t *pool = request_pool(rq);

    Result *result = (Result *) pool_malloc(pool, sizeof(Result));
    if (result == NULL)
        return &Result::out_of_memory;

    Context context(sn, rq, pool);
    *result = expr->evaluate(context);

    return result;
}
const Result *result_bool(Session *sn, Request *rq, PRBool b)
{
    pool_handle_t *pool = request_pool(rq);

    Result *result = (Result *) pool_malloc(pool, sizeof(Result));
    if (result == NULL)
        return &Result::out_of_memory;

    Context context(sn, rq, pool);
    *result = context.createBooleanResult(b == PR_TRUE);

    return result;
}
示例#29
0
文件: proxy.c 项目: Nadrin/mcproxy
int proxy_transfer(char mode, nethost_t* host, objlist_t* data, size_t datasize)
{
  if(mode == MODE_RECV) {
    data->dataptr = pool_malloc(NULL, datasize);
    if(net_recv(host->s, (char*)data->dataptr, datasize) != NETOK)
      return PROXY_ERROR;
  }
  else {
    if(net_send(host->s, (char*)data->dataptr, datasize) != NETOK)
      return PROXY_ERROR;
  }
  return PROXY_OK;
}
const Result *result_string(Session *sn, Request *rq, const char *s, int len)
{
    pool_handle_t *pool = request_pool(rq);

    Result *result = (Result *) pool_malloc(pool, sizeof(Result));
    if (result == NULL)
        return &Result::out_of_memory;

    Context context(sn, rq, pool);
    *result = context.createNewStringResult(s, len);

    return result;
}