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; }
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); }
void free_contiguous_memory(void *addr) { if (!addr) return; __free(addr, true); return; }
void zfree(void *ptr) { if (ptr == NULL) return; update_zmalloc_stat_free(__malloc_size(ptr)); __free(ptr); }
void* map_remove(Map* map, void* key) { size_t index = map->hash(key) % map->capacity; if(!map->table[index]) { return NULL; } size_t size = list_size(map->table[index]); for(size_t i = 0; i < size; i++) { MapEntry* entry = list_get(map->table[index], i); if(map->equals(entry->key, key)) { void* data = entry->data; list_remove(map->table[index], i); __free(entry, map->pool); if(list_is_empty(map->table[index])) { list_destroy(map->table[index]); map->table[index] = NULL; } map->size--; return data; } } return NULL; }
inline void free(void* ptr) { __free(ptr, __malloc_pool); #if DEBUG freed(ptr); #endif /* DEBUG */ }
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; }
/* 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"); }
void free_contiguous_memory_by_paddr(unsigned long paddr) { if (!paddr) return; __free((void *)paddr, false); return; }
static void destroy(Map* map) { for(size_t i = 0; i < map->capacity; i++) { List* list = map->table[i]; if(!list) continue; ListIterator iter; list_iterator_init(&iter, list); while(list_iterator_has_next(&iter)) { MapEntry* entry = list_iterator_next(&iter); __free(entry, map->pool); } list_destroy(list); } __free(map->table, map->pool); }
//====================================================================== // release //====================================================================== inline void __release() throw() { __free(); hmem.release(wksp,wlen); slots = 0; itmax = 0; hslot = 0; kpool.format(0,0); hpool.format(0,0); }
// Hooks free function void free(void *ptr) { extern void __free(void *); static size_t get_memory_size(void *ptr); size_t size; size = get_memory_size(ptr); malloc_mem_monitor -= size; __free(ptr); }
static void key_call_destroy(void *vp) { struct key_call_private *kcp = (struct key_call_private *)vp; if (kcp) { if (kcp->client) clnt_destroy(kcp->client); __free(kcp); } }
static inline void p_overhead_rpc_free(struct p_rpc *prpc) { #ifndef PREALLOC int i; assert(prpc != 0); for (i = 0 ; i < prpc->n; i++) { assert(prpc->rw[i].ptr != (int *)0); /* printf("f: %08x\n",(int)prpc->rw[i].ptr); */ __free(prpc->rw[i].ptr); } #endif /* !PREALLOC */ }
static void caml_ba_finalize(value v) { struct caml_ba_array * b = Caml_ba_array_val(v); switch (b->flags & CAML_BA_MANAGED_MASK) { case CAML_BA_EXTERNAL: break; case CAML_BA_MANAGED: if (b->proxy == NULL) { __free(b->data); } else { if (-- b->proxy->refcount == 0) { __free(b->proxy->data); caml_stat_free(b->proxy); } } break; case CAML_BA_MAPPED_FILE: caml_failwith("CAML_BA_MAPPED_FILE: unsupported"); break; } }
static void file_write_func() { int file_fd = 0; int file_context = 0; int size = 1024; char* buffer; __malloc_pool = malloc(0x40000); init_memory_pool(0x40000, __malloc_pool, 0); buffer = malloc(size); __fio = fio_create(__malloc_pool); timer_init("Intel(R) Core(TM) i5-4670 CPU @ 3.40GHz"); event_init(); file_init(); for(int i = 0; i < 256; i++) { file_context = file_fd = i; sprintf(buffer, "Write Test %d", file_fd); int status = file_read(file_fd, buffer, size, write_callback, &file_context); __fio->output_buffer->head = i; __fio->output_buffer->tail = i + 1; FIORequest* output_buffer = malloc(sizeof(FIORequest)); output_buffer->type = FILE_T_WRITE; output_buffer->context = &file_context; output_buffer->fd = file_fd; output_buffer->id = (__fio->request_id - 1) % FIO_MAX_REQUEST_ID; output_buffer->op.file_io.buffer = buffer; output_buffer->op.file_io.size = size; __fio->output_buffer->array[i] = output_buffer; assert_int_equal(status, FIO_OK); event_loop(); } fifo_destroy(__fio->input_buffer); fifo_destroy(__fio->output_buffer); __free(__fio, __malloc_pool); destroy_memory_pool(__malloc_pool); free(__malloc_pool); __malloc_pool = NULL; }
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; }
unsigned int kmp_search(unsigned char *S, unsigned int slen, unsigned char *W, unsigned int wlen) { unsigned int m=0, i=0; unsigned int *T; __assert(S && W); T = (unsigned int*)__malloc(wlen * sizeof(unsigned int)); __assert(T); __kmp_table(W, wlen, T); #ifdef DEBUG __kmp_test(W, wlen, T); #endif while (m+i < slen) { if (W[i] == S[m+i]) { if (i == wlen-1) { __free(T); return m; } i = i+1; } else { m = m+i-T[i]; if (T[i] > -1) i = T[i]; else i = 0; } } __free(T); return slen; }
MapEntry* map_iterator_remove(MapIterator* iter) { iter->list_index--; MapEntry* entry = list_remove(iter->map->table[iter->index], iter->list_index); iter->entry.key = entry->key; iter->entry.data = entry->data; __free(entry, iter->map->pool); if(list_is_empty(iter->map->table[iter->index])) { list_destroy(iter->map->table[iter->index]); iter->map->table[iter->index] = NULL; } iter->map->size--; return &iter->entry; }
int __fdtable_free(__fdtable_t * fdtable) { if(fdtable == 0) { errno = EINVAL; return (-1); } __free(&fdtable->Descriptors); memset(fdtable, 0, sizeof(*fdtable)); fdtable->Signature = MAGIC('B', 'A', 'A', 'D'); return (0); }
bool endpoint_free(NetworkInterface* ni, Endpoint* endpoint) { 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); } //delete if port is empty IPv4Interface* interface = ni_ip_get(ni, endpoint->addr); if(set_is_empty(interface->tcp_ports) && set_is_empty(interface->udp_ports)) { ni_ip_remove(ni, endpoint->addr); } __free(endpoint, ni->pool); return true; }
void free_tree(struct simpleNode* n){ //遍历 struct simpleNode *x = n; if (x) { //printf("==========Begin to free %s, image=%s=============\n",x->info.node_name,cJSON_Print(x->info.image)); //free_tree(x->firstChild); //__free(x); free_tree(x->firstChild); free_tree(x->nextSibling); __free(x); //__free(x); } }
static void file_opendir_func() { int file_fd = 0; int file_context = 0; __malloc_pool = malloc(0x40000); init_memory_pool(0x40000, __malloc_pool, 0); __fio = fio_create(__malloc_pool); timer_init("Intel(R) Core(TM) i5-4670 CPU @ 3.40GHz"); event_init(); file_init(); /* check context & fd unitl get to Max request id */ for(int i = 0; i < 256; i++) { file_context = i; int status = file_opendir("Test", opendir_callback, &file_context); __fio->output_buffer->head = i; __fio->output_buffer->tail = i + 1; /* FIORequest that put in to output_buffer of __fio */ FIORequest* output_buffer = malloc(sizeof(FIORequest)); output_buffer->type = FILE_T_OPENDIR; output_buffer->context = &file_context; output_buffer->id = (__fio->request_id - 1) % FIO_MAX_REQUEST_ID; output_buffer->fd = file_fd = i; __fio->output_buffer->array[i] = output_buffer; assert_int_equal(FIO_OK, status); event_loop(); } fifo_destroy(__fio->input_buffer); fifo_destroy(__fio->output_buffer); __free(__fio, __malloc_pool); destroy_memory_pool(__malloc_pool); free(__malloc_pool); __malloc_pool = NULL; }
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; }
static void file_close_func() { int file_fd = 0; int file_context = 0; __malloc_pool = malloc(0x40000); init_memory_pool(0x40000, __malloc_pool, 0); __fio = fio_create(__malloc_pool); timer_init("Intel(R) Core(TM) i5-4670 CPU @ 3.40GHz"); event_init(); file_init(); for(int i = 0; i < 256; i++) { file_context = file_fd = i; int status = file_close(file_fd, close_callback, &file_context); __fio->output_buffer->head = i; __fio->output_buffer->tail = i + 1; FIORequest* output_buffer = malloc(sizeof(FIORequest)); output_buffer->type = FILE_T_CLOSE; output_buffer->context = &file_context; output_buffer->id = (__fio->request_id - 1) % FIO_MAX_REQUEST_ID; output_buffer->fd = file_fd; __fio->output_buffer->array[i] = output_buffer; assert_int_equal(status, FIO_OK); event_loop(); } fifo_destroy(__fio->input_buffer); fifo_destroy(__fio->output_buffer); __free(__fio, __malloc_pool); destroy_memory_pool(__malloc_pool); free(__malloc_pool); __malloc_pool = NULL; }
/* * [0 ... m] [m+1 ... num-1] */ static void __merge(void *base, size_t num, size_t size, size_t m, CMP_FUNC cmpf, SWAP_FUNC swapf) { unsigned int i=0, j=m+1, c; void *ptmp, *pold; pold = ptmp = __malloc(num*size); __assert(base && ptmp && m <= num); // printf("----------> m: %d\n", m); while (i<=m && j<num) { c = cmpf((char*)base+i*size, (char*)base+j*size)<=0 ? i++ : j++; __memcpy(ptmp, (char*)base+c*size, size); ptmp = (char*)ptmp+size; } if (i<=m) __memcpy(ptmp, (char*)base+i*size, (m-i+1)*size); if (j<num) __memcpy(ptmp, (char*)base+j*size, (num-j)*size); __memcpy(base, pold, num*size); // __dump(base, num, size); __free(pold); }
static void namei_cache_remove_func(int id, char *name, int value) { START(namei,remove); __free((void *)value); STOP(namei,remove); }
bool map_put(Map* map, void* key, void* data) { if(map->size + 1 > map->threshold) { // Create new map Map map2; size_t capacity = map->capacity * 2; map2.table = __malloc(sizeof(List*) * capacity, map->pool); if(!map2.table) return false; memset(map2.table, 0x0, sizeof(List*) * capacity); map2.capacity = capacity; map2.threshold = THRESHOLD(capacity); map2.size = 0; map2.hash = map->hash; map2.equals = map->equals; map2.pool = map->pool; // Copy MapIterator iter; map_iterator_init(&iter, map); while(map_iterator_has_next(&iter)) { MapEntry* entry = map_iterator_next(&iter); if(!map_put(&map2, entry->key, entry->data)) { destroy(&map2); return false; } } // Destory destroy(map); // Paste memcpy(map, &map2, sizeof(Map)); } size_t index = map->hash(key) % map->capacity; if(!map->table[index]) { map->table[index] = list_create(map->pool); if(!map->table[index]) return false; } else { size_t size = list_size(map->table[index]); for(size_t i = 0; i < size; i++) { MapEntry* entry = list_get(map->table[index], i); if(map->equals(entry->key, key)) return false; } } MapEntry* entry = __malloc(sizeof(MapEntry), map->pool); if(!entry) { if(list_is_empty(map->table[index])) { list_destroy(map->table[index]); map->table[index] = NULL; } return false; } entry->key = key; entry->data = data; if(!list_add(map->table[index], entry)) { __free(entry, map->pool); if(list_is_empty(map->table[index])) { list_destroy(map->table[index]); map->table[index] = NULL; } return false; } map->size++; return true; }
void map_destroy(Map* map) { destroy(map); __free(map, map->pool); }
void socket_delete(NetworkInterface* ni, Socket* socket) { __free(socket, ni->pool); }