Ejemplo n.º 1
0
Map* map_create(size_t initial_capacity, uint64_t(*hash)(void*), bool(*equals)(void*,void*), void* pool) {
	if(!equals)
		equals = map_uint64_equals;

	if(!hash)
		hash = map_uint64_hash;

	size_t capacity = 1;
	while(capacity < initial_capacity)
		capacity <<= 1;

	Map* map = __malloc(sizeof(Map), pool);
	if(!map)
		return NULL;

	map->table = __malloc(sizeof(List*) * capacity, pool);
	if(!map->table) {
		__free(map, pool);
		return NULL;
	}

	memset(map->table, 0x0, sizeof(List*) * capacity);
	map->capacity = capacity;
	map->threshold = THRESHOLD(capacity);
	map->size = 0;
	map->hash = hash;
	map->equals = equals;
	map->pool = pool;

	return map;
}
Ejemplo n.º 2
0
static void malloc_func(void** state) {
	size_t pool_size = 0x40000;
/*
	void* malloc_pool = malloc(pool_size);
	init_memory_pool(pool_size, malloc_pool, 0);
	
	size_t mem_size = 0x3e000;
	void* mem = __malloc(mem_size, malloc_pool);
	assert_in_range(mem, malloc_pool, malloc_pool + pool_size);	

	printf("extra malloc\n");
	void* mem2 = __malloc(1800, malloc_pool);
	assert_in_range(mem2, malloc_pool, malloc_pool + pool_size);	

	destroy_memory_pool(malloc_pool);
	free(malloc_pool);
	malloc_pool = NULL;

	return;
*/

	/*
	 * 0x3e000 is max size that won't make failure of malloc
	 * when pool size is set to 0x4000
	 * If malloc size over the 0x3e000(may be 0x3e001), malloc failed.
	 * more problems is descirbed in Packetngin App Test Sheet.
	 */

	/* use private malloc_pool */
	for(size_t i = 1; i < pool_size - 0x3e001; i++) {
		void* malloc_pool = malloc(pool_size);
		init_memory_pool(pool_size, malloc_pool, 0);
		
		size_t mem_size = i;
		void* mem = __malloc(mem_size, malloc_pool);
		assert_in_range(mem, malloc_pool, malloc_pool + pool_size);	

		destroy_memory_pool(malloc_pool);
		free(malloc_pool);
		malloc_pool = NULL;
	}	

	/* use __malloc_pool */
	for(size_t i = 1; i < pool_size - 0x3e001; i++) {
		__malloc_pool = malloc(pool_size);
		init_memory_pool(pool_size, __malloc_pool, 0);
		
		size_t mem_size = i;
		void* mem = __malloc(mem_size, NULL);

		assert_in_range(mem, __malloc_pool, __malloc_pool + pool_size);	

		destroy_memory_pool(__malloc_pool);
		free(__malloc_pool);
		__malloc_pool = NULL;
	}	
}
Ejemplo n.º 3
0
/* [caml_ba_alloc] will allocate a new bigarray object in the heap.
   If [data] is NULL, the memory for the contents is also allocated
   (with [malloc]) by [caml_ba_alloc].
   [data] cannot point into the OCaml heap.
   [dim] may point into an object in the OCaml heap.
*/
CAMLexport value
caml_ba_alloc(int flags, int num_dims, void * data, intnat * dim)
{
  uintnat num_elts, asize, size;
  int overflow, i;
  value res;
  struct caml_ba_array * b;
  intnat dimcopy[CAML_BA_MAX_NUM_DIMS];
#if defined(__FreeBSD__) && defined(_KERNEL)
  struct caml_ba_proxy *proxy;
#endif

  Assert(num_dims >= 1 && num_dims <= CAML_BA_MAX_NUM_DIMS);
  Assert((flags & CAML_BA_KIND_MASK) <= CAML_BA_COMPLEX64);
  for (i = 0; i < num_dims; i++) dimcopy[i] = dim[i];
  size = 0;
  if (data == NULL) {
    overflow = 0;
    num_elts = 1;
    for (i = 0; i < num_dims; i++) {
      num_elts = caml_ba_multov(num_elts, dimcopy[i], &overflow);
    }
    size = caml_ba_multov(num_elts,
                          caml_ba_element_size[flags & CAML_BA_KIND_MASK],
                          &overflow);
    if (overflow) caml_raise_out_of_memory();
    data = __malloc(size);
    if (data == NULL && size != 0) caml_raise_out_of_memory();
    flags |= CAML_BA_MANAGED;
  }
  asize = SIZEOF_BA_ARRAY + num_dims * sizeof(intnat);
  res = caml_alloc_custom(&caml_ba_ops, asize, size, CAML_BA_MAX_MEMORY);
  b = Caml_ba_array_val(res);
#if defined(__FreeBSD__) && defined(_KERNEL)
  if ((flags & CAML_BA_MANAGED_MASK) != CAML_BA_MANAGED) {
    b->proxy = __malloc(sizeof(struct caml_ba_proxy));
    if (b->proxy == NULL) caml_raise_out_of_memory();
    proxy = b->proxy;

    for (proxy->size = 0, i = 0; i < num_dims; i++)
      proxy->size += dim[i];
    proxy->refcount = 1;

    if ((flags & CAML_BA_MANAGED_MASK) == CAML_BA_FBSD_MBUF) {
      proxy->type = CAML_FREEBSD_MBUF;
      proxy->data = data;
      b->data = mtod((struct mbuf *) proxy->data, void *);
    }
Ejemplo n.º 4
0
char *caml_aligned_malloc_for_minor (asize_t size, int modulo, void **block)
{
  char *raw_mem;
  uintnat aligned_mem;
                                                  Assert (modulo < Page_size);
  raw_mem = (char *) __malloc (size + Page_size);
  if (raw_mem == NULL) return NULL;
  *block = raw_mem;
  raw_mem += modulo;                /* Address to be aligned */
  aligned_mem = (((uintnat) raw_mem / Page_size + 1) * Page_size);
#ifdef DEBUG
  {
    uintnat *p;
    uintnat *p0 = (void *) *block,
            *p1 = (void *) (aligned_mem - modulo),
            *p2 = (void *) (aligned_mem - modulo + size),
            *p3 = (void *) ((char *) *block + size + Page_size);

    for (p = p0; p < p1; p++) *p = Debug_filler_align;
    for (p = p1; p < p2; p++) *p = Debug_uninit_align;
    for (p = p2; p < p3; p++) *p = Debug_filler_align;
  }
#endif
  return (char *) (aligned_mem - modulo);
}
Ejemplo n.º 5
0
void makeNodePrefixes(CFNode *nd, char *prefix) {
   char *left, *right;
   int len;

   if (nd) {
      len = strlen(prefix);

      if (nd->cf) {
         nd->cf->prefix = (char*)__malloc(sizeof(char) * len);
         strcpy(nd->cf->prefix, prefix);
      }
      else {
         left = (char*)alloca(sizeof(char) * (len + 2));
         strcpy(left, prefix);
         left[len] = '0';

         right = (char*)alloca(sizeof(char) * (len + 2));
         strcpy(right, prefix);
         right[len] = '1';

         left[len + 1] = right[len + 1] = 0;

         makeNodePrefixes(nd->left, left);
         makeNodePrefixes(nd->right, right);
      }
   }
}
Ejemplo n.º 6
0
/*
 NAME:allocate_shot_memory
 PURPOSE: Create files to store final preconditioner and gradient results. Must be initialized with zeroes.
 
 outputfolder     (in) folder where snapshot data is store
 VolumeMemory     (in) memory needed to store the domain
 
 RETURN none
 */
void create_output_volumes(char *outputfolder, integer VolumeMemory)
{
    log_info ( "Creating output files in %s", outputfolder);

#ifndef DO_NOT_PERFOM_IO    
    char fnamePrecond[300], fnameGradient[300];
    
    sprintf( fnameGradient, "%s/resultGradient.res", outputfolder);
    sprintf( fnamePrecond , "%s/resultPrecond.res", outputfolder);
    
    FILE *fGradient = safe_fopen( fnameGradient, "wb", __FILE__, __LINE__ );
    FILE *fPrecond  = safe_fopen( fnamePrecond , "wb", __FILE__, __LINE__ );
     
    int numIts = ceil( VolumeMemory / IO_CHUNK_SIZE );
    
    /* create buffer array */
    real *tmparray = (real*) __malloc( ALIGN_INT, IO_CHUNK_SIZE );
    
    /* perform the accumulation of the chunks */
    for (int i=0; i<numIts; i++) {
        safe_fwrite(tmparray, 1, IO_CHUNK_SIZE, fGradient, __FILE__, __LINE__ );
        safe_fwrite(tmparray, 1, IO_CHUNK_SIZE, fPrecond , __FILE__, __LINE__ );
    }
    
    __free(tmparray);
    
    // close files
    safe_fclose( fnameGradient, fGradient, __FILE__, __LINE__ );
    safe_fclose( fnamePrecond , fPrecond , __FILE__, __LINE__ );
#endif

		log_info ("Output volumes created correctly");
}
Ejemplo n.º 7
0
static int
__zero_segment (int envid, u_int start, u_int sz)
{
  u_int temp_pages;

  assert (!(start & PGMASK)); 

  temp_pages = (u_int)__malloc(PGROUNDUP(sz));
  if (temp_pages == 0) return -1;

  /* alloc pages for this segment and map into our address space writeable */
  if (__vm_alloc_region (temp_pages, sz, 0, PG_P|PG_U|PG_W) < 0) {
    __free((void*)temp_pages);
    return -1;
  }
  /* and map them into the other address space */
  if (__vm_share_region (temp_pages, sz, 0, 0, envid, start) < 0) {
    __free((void*)temp_pages);
    return -1;
  }
  /* zero the pages */
  bzero ((void *)temp_pages, sz);
  /* and remove our mapping of the pages */
  if (__vm_free_region (temp_pages, sz, 0) < 0) {
    __free((void*)temp_pages);
    return -1;
  }

  __free((void*)temp_pages);
  return 0;
}  
Ejemplo n.º 8
0
int register_pagefault_handler (uint vastart, int len,
				int (*handler)(uint,int))
{
   handler_t *tmp = handlers;
   uint vaend = PGROUNDUP (vastart+len);

   vastart = PGROUNDDOWN (vastart);

   while (tmp) {
      if ((vastart >= tmp->vastart) && (vastart < tmp->vaend)) {
         return (-1);
      }
      if ((vaend >= tmp->vastart) && (vaend < tmp->vaend)) {
         return (-1);
      }
      if ((vastart < tmp->vastart) && (vaend >= tmp->vaend)) {
         return (-1);
      }
      tmp = tmp->next;
   }

   tmp = (handler_t *) __malloc (sizeof(handler_t));
   assert(tmp);
   tmp->vastart = vastart;
   tmp->vaend = vaend;
   tmp->handler = handler;
   tmp->next = handlers;
   handlers = tmp;

   /* kprintf ("(%d) pagefault_handler registered for %x -- %x\n", geteid(),
      tmp->vastart, tmp->vaend); */

   return (0);
}
Ejemplo n.º 9
0
void *__realloc(void *ptr, size_t size, const char *file, int line)
{
    __maldbg_header_t *header;

    if (ptr == NULL)
        return __malloc(size, file, line);
    else
    {
        header = (__maldbg_header_t*) ptr - 1;

		LmuxAcquire(&maldbg_mux);
		assert(__malloc_check_header(header));

        header = realloc(header, sizeof(__maldbg_header_t) + size);

        if (header->next == NULL)
            maldbg_last = header;
        else
            header->next->prev = header;

        if (header->prev == NULL)
            maldbg_first = header;
        else
            header->prev->next = header;
		LmuxRelease(&maldbg_mux);

		header->size = sizeof(__maldbg_header_t) + size;
		header->magic[1] = header->magic[0] ^ header->size;
		assert(__malloc_check_header(header));
        return header + 1;
    }
}
Ejemplo n.º 10
0
static inline struct p_rpc *
p_overhead_rpc_alloc(int size,int count) {
    int i,number;
#ifdef PREALLOC
    static char space[MAX_P_RPC][1514];
#endif /* PREALLOC */
    number = (count / size) + ((count % size) ? 1 : 0);
    DPRINTF(CLUHELP_LEVEL,("number: %d size: %d count: %d\n",number,size,count));
    overhead_p_rpc.n = number;
    size+=NFS_SLACK_SPACE;		
    for (i = 0 ; i < number; i++) {
	overhead_p_rpc.rw[i].size = size;
#ifdef PREALLOC 
	overhead_p_rpc.rw[i].ptr = (int *)&space[i][0];
#else
	overhead_p_rpc.rw[i].ptr = (int *)__malloc(size); 
#endif /* PREALLOC */
	/* 	printf("m: %08x\n",(int)overhead_p_rpc.rw[i].ptr);   */

	overhead_p_rpc.rw[i].start = overhead_p_rpc.rw[i].ptr;
	overhead_p_rpc.rw[i].end = overhead_p_rpc.rw[i].ptr;

	/* this assert can go later */
	assert(overhead_p_rpc.rw[i].start != (int *)0);
	if (overhead_p_rpc.rw[i].start == (int *)0) {
	    return (struct p_rpc *)0;
	}
    }
    return &overhead_p_rpc;
}
Ejemplo n.º 11
0
void inicializar_cache(const configuracion* conf)
{
    int i, j;

    num_conjuntos = conf->num_lineas / conf->asociatividad;
    asociatividad = conf->asociatividad;
    es_lru = conf->reemplazo;

    num_bits_offset = log_base_2(conf->tamano_linea);
    num_bits_indice = log_base_2(num_conjuntos);
    num_bits_etiqueta = 32 - num_bits_offset - num_bits_indice;

    /* inicializamos la caché, de forma que todas las líneas esten disponibles */
    cache = __malloc(linea*, num_conjuntos);
    for(i = 0; i != num_conjuntos; ++i)
        cache[i] = __malloc(linea, asociatividad);

    for(i = 0;i != num_conjuntos; ++i)
        for(j = 0; j != asociatividad; ++j)
        {
            linea* l = &cache[i][j];
            l->contador_lru = 1;
            l->valido = 0;
        }
}
Ejemplo n.º 12
0
void *malloc(size_t size)
{
#ifdef MALLOC_PC
  return __malloc_pc(size, __builtin_return_address(0));
#else
	return(__malloc(size));
#endif
}
Ejemplo n.º 13
0
uintnat caml_ba_deserialize(void * dst)
{
  struct caml_ba_array * b = dst;
  int i, elt_size;
  uintnat num_elts;

  /* Read back header information */
  b->num_dims = caml_deserialize_uint_4();
  b->flags = caml_deserialize_uint_4() | CAML_BA_MANAGED;
  b->proxy = NULL;
  for (i = 0; i < b->num_dims; i++) b->dim[i] = caml_deserialize_uint_4();
  /* Compute total number of elements */
  num_elts = caml_ba_num_elts(b);
  /* Determine element size in bytes */
#ifdef _KERNEL
  if ((b->flags & CAML_BA_KIND_MASK) > CAML_BA_NATIVE_INT)
#else
  if ((b->flags & CAML_BA_KIND_MASK) > CAML_BA_COMPLEX64)
#endif
    caml_deserialize_error("input_value: bad bigarray kind");
  elt_size = caml_ba_element_size[b->flags & CAML_BA_KIND_MASK];
  /* Allocate room for data */
  b->data = __malloc(elt_size * num_elts);
  if (b->data == NULL)
    caml_deserialize_error("input_value: out of memory for bigarray");
  /* Read data */
  switch (b->flags & CAML_BA_KIND_MASK) {
  case CAML_BA_SINT8:
  case CAML_BA_UINT8:
    caml_deserialize_block_1(b->data, num_elts); break;
  case CAML_BA_SINT16:
  case CAML_BA_UINT16:
    caml_deserialize_block_2(b->data, num_elts); break;
#ifdef _KERNEL
#else
  case CAML_BA_FLOAT32:
#endif
  case CAML_BA_INT32:
    caml_deserialize_block_4(b->data, num_elts); break;
#ifdef _KERNEL
#else
  case CAML_BA_COMPLEX32:
    caml_deserialize_block_4(b->data, num_elts * 2); break;
  case CAML_BA_FLOAT64:
#endif
  case CAML_BA_INT64:
    caml_deserialize_block_8(b->data, num_elts); break;
#ifdef _KERNEL
#else
  case CAML_BA_COMPLEX64:
    caml_deserialize_block_8(b->data, num_elts * 2); break;
#endif
  case CAML_BA_CAML_INT:
  case CAML_BA_NATIVE_INT:
    caml_ba_deserialize_longarray(b->data, num_elts); break;
  }
  return sizeof(struct caml_ba_array) + (b->num_dims - 1) * sizeof(intnat);
}
Ejemplo n.º 14
0
static nezvm_string_ptr_t Loader_ReadString(ByteCodeLoader *loader) {
  uint32_t len = Loader_Read16(loader);
  nezvm_string_ptr_t str = (nezvm_string_ptr_t)__malloc(sizeof(*str) - 1 + len);
  str->len = len;
  for (uint32_t i = 0; i < len; i++) {
    str->text[i] = Loader_Read32(loader);
  }
  return str;
}
Ejemplo n.º 15
0
void *hm_alloc(size_t size)
{
	void *ptr;

	ptr = __malloc(size);
	if (!ptr)
		BUG();

	return ptr;
}
Ejemplo n.º 16
0
void *hm_alloc0(size_t size)
{
	void *ptr;

	ptr = __malloc(size);
	if (!ptr)
		BUG();

	memset(ptr, 0, size);
	return ptr;
}
Ejemplo n.º 17
0
Socket* socket_create(NetworkInterface* ni, SP* sp, SA* sa) {
	Socket* socket = __malloc(sizeof(socket), ni->pool);
	if(!socket)
		return NULL;

	socket->sp = sp;
	socket->sa = sa;
	socket->fin = false;

	return socket;
}
Ejemplo n.º 18
0
void *zmalloc(size_t size)
{
	void *ptr = __malloc(size);

	if (!ptr)
		zmalloc_oom_handler(size);

	update_zmalloc_stat_alloc(__malloc_size(ptr));
	// 分配后将其清零.
	memset(ptr, 0, size);
	return ptr;
}
Ejemplo n.º 19
0
static void free_func() {
	size_t pool_size = 0x40000;
	size_t malloc_size = 253951;
	void* mem;

	/* use private pool */
	void* malloc_pool = malloc(pool_size);
	init_memory_pool(pool_size, malloc_pool, 0);
	
	mem = __malloc(malloc_size, malloc_pool);			
	__free(mem, malloc_pool);
	mem = NULL;

	mem = __malloc(malloc_size, malloc_pool);
	assert_non_null(mem);
	__free(mem, malloc_pool);

	destroy_memory_pool(malloc_pool);
	free(malloc_pool);
	malloc_pool = NULL;

	/* use __malloc_pool */
	__malloc_pool = malloc(pool_size);
	init_memory_pool(pool_size, __malloc_pool, 0);	

	mem = __malloc(malloc_size, __malloc_pool);
	__free(mem, __malloc_pool);
	mem = NULL;

	mem = __malloc(malloc_size, __malloc_pool);
	assert_non_null(mem);
	__free(mem, __malloc_pool);
	mem = NULL;

	destroy_memory_pool(__malloc_pool);
	free(__malloc_pool);
	__malloc_pool = NULL;
}
Ejemplo n.º 20
0
// Hooks malloc function
void *malloc(size_t size)
{
  extern void *__malloc(size_t);
  void *ptr;

  ptr = __malloc(size);
  if (ptr)
  {
    malloc_mem_monitor += size;
    if (malloc_mem_monitor > malloc_mem_monitor_max)
      malloc_mem_monitor_max = malloc_mem_monitor;
  }
  return ptr;
}
Ejemplo n.º 21
0
static u_int read_write_memory (int request,u_int env, u_int addr, int data) {
  u_int write;
  u_int pte;
  u_int offset;
  u_int va = (u_int )addr;
  u_int temp_page;
  int r;

#define PT_WRITE_D 1
#define PT_WRITE_I 1
#define PT_READ_D 0
#define PT_READ_I 0

  if (request == PT_WRITE_D || request == PT_WRITE_I) {
    write = 1;
  } else {
    write = 0;
  }

  assert (env);
  if (!(pte = sys_read_pte (va, 0, env, &r))) {
    printf ("ptrace: couldn't read pte (va %x, env %x)\n", va, env);
    return (0);
  }
  temp_page = (u_int)__malloc(NBPG);
  if (temp_page == 0 ||
      _exos_self_insert_pte (0, write ? pte | PG_W : pte, temp_page,
			     ESIP_DONTPAGE, NULL) < 0) {
    printf ("ptrace: couldn't map page\n");
    __free((void*)temp_page);
    return (0);
  }
  offset = va & PGMASK;
  va = temp_page+offset;

  if (write) {
    *(int *)va = data;
  } else {
    data = *(int *)va;
  }

  if (_exos_self_unmap_page (0, temp_page) < 0) {
    printf ("ptrace: couldn't unmap page\n");
    __free((void*)temp_page);
    assert (0);
  }

  __free((void*)temp_page);
  return (data);
}
Ejemplo n.º 22
0
wchar_t *__wcsdup(const wchar_t* str, const char *file, int line)
{
    wchar_t *ret;
    
    if (str == NULL)
	str = L"";

    ret = __malloc((wcslen(str) + 1) * sizeof(wchar_t), file, line);

    if (!ret)
	return NULL;

    wcscpy(ret, str);
    return ret;
}
Ejemplo n.º 23
0
char *__strdup(const char* str, const char *file, int line)
{
    char *ret;
    
    if (str == NULL)
	str = "";

    ret = __malloc(strlen(str) + 1, file, line);

    if (!ret)
	return NULL;

    strcpy(ret, str);
    return ret;
}
Ejemplo n.º 24
0
static void nfs_unlinkatend(char *name, struct nfs_fh *fhandle) {
  struct uae *p;

  //fprintf(stderr,"NFS_UNLINKATEND name: %s\n",name);
  p = (struct uae *)__malloc(sizeof (struct uae));
  if (p == NULL) {
    fprintf(stderr,
	      "could not allocate space for removing nfs file while it is referenced\n");
    return;
  }
  strcpy(&p->name[0], name);
  memcpy(&p->fh, fhandle, sizeof(struct nfs_fh));
  
  p->next = uae_base;
  uae_base = p;
  
}
Ejemplo n.º 25
0
void *__realloc(void *ptr, size_t size, const char *file, int line)
{
	void *new_ptr;
	block_t *old_block;

	new_ptr = __malloc(size, file, line);
	if (new_ptr == NULL)
		return NULL;

	if (ptr != NULL)
	{
		old_block = (block_t*) ptr - 1;
		memcpy(new_ptr, ptr, min(old_block->allocated.size, size));
		free(ptr);
	}

	return new_ptr;
}
Ejemplo n.º 26
0
static int
exec_emul_openbsd(int fd, const char *path, char *const argv[],
		  char *const envp[], struct Env *e, u_int flags)
{
  // u_int envid = e->env_id;
  char **newv = (char**) argv;
  int ret;
  char *extra_argv_space;

  if (!is_emul_openbsd(fd)) return 0;

  if (!(flags & _EXEC_USE_FD)) close(fd);

  while(*newv++);
  extra_argv_space = (char *)__malloc((char *)newv - (char *)argv +
				      2 * sizeof(char*));
  if (!extra_argv_space) {
    fprintf(stderr,"dynamic loader could not allocate extra argv space "
	    "for copying args for emulator\n");
    ret = -ENOMEM;
    goto exec_emul_openbsd_done;
  }

  newv = (char **)extra_argv_space;
  *newv = getenv("EMULATOR");
  if (!newv[0]) newv[0] = "/bin/emulate";
  newv++;

  *newv++ = (char *)path;
  argv++;
  while(*argv != (char *) 0)
    *newv++ = *argv++;
  /* copy the 0 */
  *newv = *argv;

  argv = (char **)extra_argv_space;

  ret = fork_execve0_part2(argv[0], argv, envp, e, -1, flags);

  __free(extra_argv_space);

 exec_emul_openbsd_done:
  return ret;
}
Ejemplo n.º 27
0
Endpoint* endpoint_alloc(NetworkInterface* ni, uint32_t addr, uint8_t protocol, uint16_t port) {
	if(!ni_ip_get(ni, addr)) { 
		if(!ni_ip_add(ni, addr))
			return NULL;
	}

	if(protocol == IP_PROTOCOL_TCP) {
		if(!tcp_port_alloc0(ni, addr, port))
			goto port_alloc_fail;
	} else if(protocol == IP_PROTOCOL_UDP) {
		if(!udp_port_alloc0(ni, addr, port))
			goto port_alloc_fail;
	}

	Endpoint* endpoint = __malloc(sizeof(Endpoint), ni->pool);
	if(!endpoint)
		goto endpoint_alloc_fail;

	endpoint->ni = ni;
	endpoint->addr = addr;
	endpoint->protocol = protocol;
	endpoint->port = port;

	return endpoint;

endpoint_alloc_fail:
	//port free
	if(endpoint->protocol == IP_PROTOCOL_TCP) {
		tcp_port_free(ni, endpoint->addr, endpoint->port);
	} else if (endpoint->protocol == IP_PROTOCOL_UDP) {
		udp_port_free(ni, endpoint->addr, endpoint->port);
	}

port_alloc_fail:
	;
	//delete if port is empty
	IPv4Interface* interface = ni_ip_get(ni, addr);
	if(set_is_empty(interface->tcp_ports) && set_is_empty(interface->udp_ports)) {
		ni_ip_remove(ni, addr);
	}

	return NULL;
}
Ejemplo n.º 28
0
static void realloc_func() {
	size_t pool_size = 0x40000;
	size_t malloc_size;

	for(size_t i = 1; i < pool_size - 0x3e001; i++) {
		malloc_size = i;

		void* malloc_pool = malloc(pool_size);
		init_memory_pool(pool_size, malloc_pool, 0);

		void* mem = __malloc(malloc_size, malloc_pool);		
		assert_in_range(mem, malloc_pool, malloc_pool + pool_size);

		mem = __realloc(mem, 0x3e000, malloc_pool);
		assert_in_range(mem, malloc_pool, malloc_pool + pool_size);

		destroy_memory_pool(malloc_pool);
		free(malloc_pool);
		malloc_pool = NULL;
	}
}
Ejemplo n.º 29
0
inline void* malloc(size_t size) {
	void* ptr = __malloc(size, __malloc_pool);
	
	if(!ptr) {
		// TODO: print to stderr
		printf("Not enough local memory!!!\n");
		printf("Requested size: %ld from %p\n", size, ((void**)read_rbp())[1]);
		
		#if DEBUG
		malloc_statistics();
		#endif /* DEBUG */
		
		while(1) asm("hlt");
	}
	
	#if DEBUG
	malloced(((void**)read_rbp())[1], size, ptr);
	#endif /* DEBUG */
	
	return ptr;
}
Ejemplo n.º 30
0
CFTree * constructCFTree(CFLL *ll) {
   CFTree *tree = (CFTree*)__malloc(sizeof(CFTree));
   CFNode *nd, *nd1, *nd2;

   if (ll) {
      while (ll->size > 1) {
         nd1 = removeCFNode(ll);
         nd2 = removeCFNode(ll);
         nd = constructCFNode(NULL);

         nd->left = nd1;
         nd->right = nd2;
         nd->value = nd1->value + nd2->value;
         placeCFNode(ll, nd);
      }
      tree->root = removeCFNode(ll);

      makeTreePrefixes(tree);
   }

   return tree;
}