Example #1
0
// allocate s bytes within region r
void * _reap_malloc(struct _ReapHandle *r, _AliasQualHandle_t aq, unsigned int s) {
  void *result;
  if(r->curr == NULL) { 
    //grab a page, and hold onto a ref. We need to free it later
    void *p =malloc(default_region_page_size+sizeof(struct _ReapPage));
    if(!p)
      throw(1)
    r->curr = (struct _ReapPage*)p;
    r->curr->bget_page = (p + sizeof(struct _ReapPage));
    r->curr->next = NULL;
    //pass the page to bget
    r->bkey = bget_init_region(r->curr->bget_page, default_region_page_size);
    if(!r->bkey) 
      throw(2)
    r->bkey->priv = r;
    // direct allocation for large buffers is a pain ... revisit
    // no direct allocation means there is no need for a release function
    // no compaction, default size for acquisition request is page_size+
    bectl(r->bkey, NULL, _acquire_reap_page, NULL, default_region_page_size);
  }
  if(aq == CYC_CORE_REFCNT_AQUAL) {
    result = bgetz(r->bkey, (bufsize)(s + sizeof(int)));
    if(!result)
      throw(3)
    *((int*)result) = 1;
    result += sizeof(int);
    return (void*)result;
  }
  else {
    result = bgetz(r->bkey, (bufsize)s);
    if(!result)
      throw(4)
    return (void*)result;
  }
}
Example #2
0
void malloc_init()
{
	// initialize the bget allocator

	bectl(
		NULL, // compaction callback
		malloc_bget_acquire_callback, // alloc callback
		malloc_bget_release_callback, // free callback
		1048576 // heap-expansion increment
	);

	mutex_init(&malloc_lock);
}