void gm_free(void* ptr) { assert(GM); assert(GM->mspace_ptr); futex_lock(&GM->lock); mspace_free(GM->mspace_ptr, ptr); futex_unlock(&GM->lock); }
static void free_atfork(void* mem, const void *caller) { void *vptr = NULL; struct malloc_arena *ar_ptr; mchunkptr p; /* chunk corresponding to mem */ if (mem == 0) /* free(0) has no effect */ return; p = mem2chunk(mem); if (is_mmapped(p)) { /* release mmapped memory. */ ar_ptr = arena_for_mmap_chunk(p); munmap_chunk(arena_to_mspace(ar_ptr), p); return; } ar_ptr = arena_for_chunk(p); tsd_getspecific(arena_key, vptr); if(vptr != ATFORK_ARENA_PTR) (void)mutex_lock(&ar_ptr->mutex); mspace_free(arena_to_mspace(ar_ptr), mem); if(vptr != ATFORK_ARENA_PTR) (void)mutex_unlock(&ar_ptr->mutex); }
void palHeapAllocator::Deallocate(void* ptr) { if (ptr) { uint32_t reported_size = mspace_usable_size(ptr); mspace_free(internal_, ptr); ReportMemoryDeallocation(ptr, reported_size); } }
void memory_free(void* mem) { if (!memory_table) return; if (!use_allocator) { FREE_ALIGN(mem); return; } int mem_idx; if (memory_is_shmem(mem, &mem_idx)) { ASSERT((mem_idx >= 0 && mem_idx < memory_table_size)); memory_t* memptr = &memory_table[mem_idx]; if (memptr->in_use == 1) { REMOVE_BOUNDS(mem); mspace_free(memptr->shm_mspace, mem); return; } } ERROR("Memory address not allocated / registered with EPLIB %p, use __libc_free\n", mem); }
void free(void* mem) { if(mem < sm_lower || mem >= sm_upper) { return; } if(unlikely(sm_mspace == NULL)) return; mspace_free(sm_mspace, mem); }
/** * release memory previously allocated at ADDR */ void shmemi_mem_free (void *addr) { mspace_free (myspace, addr); #ifdef HAVE_FEATURE_DEBUG debug_alloc_del (addr); #endif /* HAVE_FEATURE_DEBUG */ }
void Impl_malloc_zone_free(malloc_zone_t *zone, void *ptr) { OSMemoryZone* osm = (OSMemoryZone*)zone; if (osm->global_zone) { return free(ptr); } else { return mspace_free(osm->memory_space, ptr); } }
void mempool_free(void * ptr) { if (mp_mspace__ && ptr) { KIRQL irql; KeAcquireSpinLock(&mp_spinlock__, &irql); mspace_free(mp_mspace__, ptr); KeReleaseSpinLock(&mp_spinlock__, irql); } }
void qxl_free (struct qxl_mem *mem, void *d) { mspace_free (mem->space, d); #ifdef DEBUG_QXL_MEM #ifdef DEBUG_QXL_MEM_VERBOSE fprintf(stderr, "free %p\n", d); #endif VALGRIND_FREELIKE_BLOCK(d, 0); #endif }
void hbw_free(void *ptr) { if (myhbwmalloc_mspace == NULL) { if (!myhbwmalloc_hardfail) { fprintf(stderr, "hbwmalloc: mspace invalid - allocating from default heap\n"); return free(ptr); } else { fprintf(stderr, "hbwmalloc: mspace invalid - cannot allocate from hbw heap\n"); abort(); } } mspace_free(myhbwmalloc_mspace, ptr); }
static void free_starter(void* mem, const void *caller) { if (mem) { mchunkptr p = mem2chunk(mem); void *msp = arena_to_mspace(&main_arena); if (is_mmapped(p)) munmap_chunk(msp, p); else mspace_free(msp, mem); } THREAD_STAT(++main_arena.stat_starter); }
int mkpath(mspace mp, const char *s, mode_t mode) { char *q, *r = NULL, *path = NULL, *up = NULL; int rv; rv = -1; if (strcmp(s, ".") == 0 || strcmp(s, "/") == 0) return (0); if ((path = mspace_strdup(mp, s)) == NULL) return rv; if ((q = mspace_strdup(mp, s)) == NULL) return rv; if ((r = dirname(q)) == NULL) goto out; if ((up = mspace_strdup(mp, r)) == NULL) return rv; if ((mkpath(mp, up, mode) == -1) && (errno != EEXIST)) goto out; if ((mkdir(path, mode) == -1) && (errno != EEXIST)) rv = -1; else rv = 0; out: if (up != NULL) mspace_free(mp, up); mspace_free(mp, q); mspace_free(mp, path); return rv; }
void* OOBase::ArenaAllocator::reallocate(void* ptr, size_t bytes, size_t align) { if (align <= 8) return mspace_realloc(m_mspace,ptr,bytes); if (mspace_realloc_in_place(m_mspace,ptr,bytes)) return ptr; void* new_ptr = mspace_memalign(m_mspace,align,bytes); if (new_ptr) { memcpy(new_ptr,ptr,mspace_usable_size(ptr)); mspace_free(m_mspace,ptr); } return new_ptr; }
METHODPREFIX(CLASS, jobject, dlalloc)(ST_ARGS, jlong size, jint align) { MYHEADID(JNISTRUCT, FID); void * ret=mspace_malloc(str->space, size); char * base=(char *)(str->base); if(ret<str->base || ret>=base+str->size) { // mspace is filled and implementation backed to stdc malloc // Our specification does not let this happen mspace_free(str->space, ret); JNU_ThrowByName(env, EXCCLASS_OUT, "Out of mspace area"); return NULL; } memset ( ret, 0, size ); return env->NewDirectByteBuffer(ret, size); }
void HeapFree(MemAllocHeap* heap, void *ptr) { bool thread_safe = 0 != (heap->m_Flags & HeapFlags::kThreadSafe); if (thread_safe) { MutexLock(&heap->m_Lock); } #if ENABLED(USE_DLMALLOC) mspace_free(heap->m_MemSpace, ptr); #else free(ptr); #endif if (thread_safe) { MutexUnlock(&heap->m_Lock); } }
void MemoryHeap::free(void *ptr) { if (mMspace == NULL) return; auto itr = mPages.find(ptr); if (itr != mPages.end()) { alloc_info info = (*itr).second; if (info.largeAlloc) { mMUnmapFun(ptr, info.size); mark_unallocated(ptr, info.size); mBytesUsed -= info.size; return; } } mBytesUsed -= mspace_usable_size(ptr); lassert(mspace_usable_size(ptr)); mspace_free(mMspace, ptr); }
void public_fREe(void* mem) { struct malloc_arena* ar_ptr; mchunkptr p; /* chunk corresponding to mem */ void (*hook) (void *, const void *) = __free_hook; if (hook != NULL) { (*hook)(mem, RETURN_ADDRESS (0)); return; } if (mem == 0) /* free(0) has no effect */ return; p = mem2chunk(mem); if (is_mmapped(p)) { /* release mmapped memory. */ ar_ptr = arena_for_mmap_chunk(p); munmap_chunk(arena_to_mspace(ar_ptr), p); return; } ar_ptr = arena_for_chunk(p); #if THREAD_STATS if(!mutex_trylock(&ar_ptr->mutex)) ++(ar_ptr->stat_lock_direct); else { (void)mutex_lock(&ar_ptr->mutex); ++(ar_ptr->stat_lock_wait); } #else (void)mutex_lock(&ar_ptr->mutex); #endif mspace_free(arena_to_mspace(ar_ptr), mem); (void)mutex_unlock(&ar_ptr->mutex); }
/* * Frees the first numPtrs objects in the ptrs list and returns the * amount of reclaimed storage. The list must contain addresses all in * the same mspace, and must be in increasing order. This implies that * there are no duplicates, and no entries are NULL. */ size_t dvmHeapSourceFreeList(size_t numPtrs, void **ptrs) { HS_BOILERPLATE(); if (numPtrs == 0) { return 0; } assert(ptrs != NULL); assert(*ptrs != NULL); Heap* heap = ptr2heap(gHs, *ptrs); size_t numBytes = 0; if (heap != NULL) { mspace msp = heap->msp; // Calling mspace_free on shared heaps disrupts sharing too // much. For heap[0] -- the 'active heap' -- we call // mspace_free, but on the other heaps we only do some // accounting. if (heap == gHs->heaps) { // mspace_merge_objects takes two allocated objects, and // if the second immediately follows the first, will merge // them, returning a larger object occupying the same // memory. This is a local operation, and doesn't require // dlmalloc to manipulate any freelists. It's pretty // inexpensive compared to free(). // ptrs is an array of objects all in memory order, and if // client code has been allocating lots of short-lived // objects, this is likely to contain runs of objects all // now garbage, and thus highly amenable to this optimization. // Unroll the 0th iteration around the loop below, // countFree ptrs[0] and initializing merged. assert(ptrs[0] != NULL); assert(ptr2heap(gHs, ptrs[0]) == heap); countFree(heap, ptrs[0], &numBytes); void *merged = ptrs[0]; for (size_t i = 1; i < numPtrs; i++) { assert(merged != NULL); assert(ptrs[i] != NULL); assert((intptr_t)merged < (intptr_t)ptrs[i]); assert(ptr2heap(gHs, ptrs[i]) == heap); countFree(heap, ptrs[i], &numBytes); // Try to merge. If it works, merged now includes the // memory of ptrs[i]. If it doesn't, free merged, and // see if ptrs[i] starts a new run of adjacent // objects to merge. if (mspace_merge_objects(msp, merged, ptrs[i]) == NULL) { mspace_free(msp, merged); merged = ptrs[i]; } } assert(merged != NULL); mspace_free(msp, merged); } else { // This is not an 'active heap'. Only do the accounting. for (size_t i = 0; i < numPtrs; i++) { assert(ptrs[i] != NULL); assert(ptr2heap(gHs, ptrs[i]) == heap); countFree(heap, ptrs[i], &numBytes); } } } return numBytes; }
void inline free(void* ptr){ mspace_free( mspaces[ mspaceId ], ptr ); }
METHODPREFIX(CLASS, void, dlfree)(ST_ARGS, jobject byteBuffer) { MYHEADID(JNISTRUCT, FID); void * ptr=env->GetDirectBufferAddress(byteBuffer); mspace_free(str->space, ptr); }
void panel_article_update(struct env_t *_SERVER) { tpl_t *tpl; TCLIST *arr; char *html, path[128], buf[128]; sqlite3_stmt *stmt; time_t visit; int rc, i, type, pos, len, n; const char *id, *title, *sortlevel, *content, *recommend, *keyword, *filename, *file, *post_time; if(tcmapget2(_SERVER->_POST, "Article_Update")) { file = tcmapget2(_SERVER->_COOKIE, "file"); id = tcmapget2(_SERVER->_POST, "id"); title = tcmapget2(_SERVER->_POST, "title"); content = tcmapget2(_SERVER->_POST, "content"); keyword = tcmapget2(_SERVER->_POST, "keyword"); filename = tcmapget2(_SERVER->_POST, "filename"); sortlevel = tcmapget2(_SERVER->_POST, "sortlevel"); recommend = tcmapget2(_SERVER->_POST, "recommend"); post_time = tcmapget2(_SERVER->_POST, "post_time"); pos = strpos(content, "<!-- idx -->"); pos = (pos == -1) ? strlen(content) : pos; rc = sqlite3_prepare(db, "UPDATE article SET title = ?, content = ?, catid = ?, keyword = ?, filename = ?, recommend = ?, position = ? WHERE art_id = ?;", -1, &stmt, NULL); sqlite3_bind_text(stmt, 1, title, -1, SQLITE_STATIC); sqlite3_bind_text(stmt, 2, content, -1, SQLITE_STATIC); sqlite3_bind_text(stmt, 3, sortlevel, -1, SQLITE_STATIC); sqlite3_bind_text(stmt, 4, keyword, -1, SQLITE_STATIC); sqlite3_bind_text(stmt, 5, filename, -1, SQLITE_STATIC); sqlite3_bind_int(stmt, 6, atoi(recommend)); sqlite3_bind_int(stmt, 7, pos); sqlite3_bind_int(stmt, 8, atoi(id)); sqlite3_step(stmt); sqlite3_finalize(stmt); visit = atoi(post_time); if(file) { memset(buf, 0, sizeof(buf)); memset(path, 0, sizeof(path)); strftime(buf, sizeof(buf), "%Y/%m/%d", localtime(&visit)); //如果有上传的文件,就把上传的文件,从临时目录移动目标目录 len = snprintf(path, sizeof(path), "./attachment/%s/%llu/", buf, (long long unsigned int)id); if(!is_dir(path)) mkpath(_SERVER->mp, path, 0777); if(!strchr(file, '|')) { memset(buf, 0, sizeof(buf)); snprintf(buf, sizeof(buf), "./files/%s", file); strcat(path, file); // log_debug("panel_article_insert buf = %s\tpath = %s\r\n", buf, path); if(file_exists(buf)) rename(buf, path); } else { arr = explode("|", (char *)file); n = tclistnum(arr); for(i=0; i<n; i++) { memset(buf, 0, sizeof(buf)); filename = tclistval2(arr, i); snprintf(buf, sizeof(buf), "./files/%s", filename); path[len] = '\0'; strcat(path, filename); // log_debug("panel_article_insert explode buf = %s\tpath = %s\r\n", buf, path); if(file_exists(buf)) rename(buf, path); } tclistdel(arr); } path[len] = '\0'; //更新图片附件的 路径 rc = sqlite3_prepare(db, "UPDATE article SET content = replace(content, '/files/', ?) WHERE art_id = ?;", -1, &stmt, NULL); sqlite3_bind_text(stmt, 1, path+1, -1, SQLITE_STATIC); sqlite3_bind_int(stmt, 2, atoi(id)); sqlite3_step(stmt); sqlite3_finalize(stmt); } #ifdef _BUILD_HTML memset(path, 0, sizeof(path)); snprintf(path, sizeof(path), "./html/article/%s/%s.htm", sortlevel, id); if(file_exists(path)) remove(path); memset(path, 0, sizeof(path)); snprintf(path, sizeof(path), "./html/list/%s", sortlevel); ftw(path, fn, 500); #endif printf("Content-type: text/html\r\n\r\n<script>alert('编辑成功!');window.location.href='/panel-article-list-%s.html';</script>", sortlevel); return ; } id = tclistval2(_SERVER->_GET, 3); if(id == NULL || !is_digit(id)) { puts("Content-type: text/html\r\n\r\n<script>alert('请您选择要更新的文章');window.location.href='/panel-article-list-all.html';</script>"); return ; } tpl = tpl_alloc(); if (tpl_load(tpl, "./templets/panel/article_update.html") != TPL_OK) { printf("Content-type: text/html\r\n\r\n./templets/panel/article_update.html Error loading template file!"); tpl_free(tpl); return ; } //加载分类的数据 rc = sqlite3_prepare(db, "SELECT sortname, sortdir FROM category", -1, &stmt, NULL); tpl_select_section(tpl, "classic"); while((rc = sqlite3_step(stmt)) == SQLITE_ROW) { for(i=0; i<sqlite3_column_count(stmt); i++) { tpl_set_field(tpl, sqlite3_column_name(stmt,i) ,sqlite3_column_text(stmt,i), strlen(sqlite3_column_text(stmt,i))); } tpl_append_section(tpl); } tpl_deselect_section(tpl); sqlite3_finalize(stmt); //加载需要编辑的数据 rc = sqlite3_prepare(db, "SELECT * FROM article WHERE art_id = ?", -1, &stmt, NULL); sqlite3_bind_int(stmt, 1, atoi(id)); while((rc = sqlite3_step(stmt)) == SQLITE_ROW) { for(i=0; i<sqlite3_column_count(stmt); i++) { type = sqlite3_column_type(stmt, i); switch(type) { case SQLITE_INTEGER: tpl_set_field_uint_global(tpl, sqlite3_column_name(stmt,i), sqlite3_column_int(stmt, i)); break; case SQLITE_TEXT: tpl_set_field_global(tpl, sqlite3_column_name(stmt,i) ,sqlite3_column_text(stmt,i), strlen(sqlite3_column_text(stmt,i))); break; } } } sqlite3_finalize(stmt); html = mspace_malloc(_SERVER->mp, tpl_length(tpl) + 1); tpl_get_content(tpl, html); printf("Content-type: text/html\r\n\r\n%s", html); tpl_free(tpl); mspace_free(_SERVER->mp, html); tpl = NULL; }
void texFree(void *p) { mspace_free(tex_msp, p); }
void panel_article_list(struct env_t *_SERVER) { tpl_t *tpl; char url[128]; char *html, *sql; int rc, i, type, total; uint limit[2], cur; sqlite3_stmt *stmt; const char *sortlevel, *page; tpl = tpl_alloc(); if (tpl_load(tpl, "./templets/panel/article_list.html") != TPL_OK) { printf("Content-type: text/html\r\n\r\n./templets/panel/article_list.html Error loading template file!"); tpl_free(tpl); return ; } total = 0; sortlevel = tclistval2(_SERVER->_GET, 3); page = tclistval2(_SERVER->_GET, 4); memset(&url, 0, sizeof(url)); if((sortlevel) && (strcmp(sortlevel, "all") == 0)) { strcpy(url, "/panel-article-list-all"); rc = sqlite3_prepare(db, "SELECT COUNT(*) AS c FROM article;", -1, &stmt, NULL); } else if((sortlevel) && (is_alpha(sortlevel)) && (strcmp(sortlevel, "html") != 0)) { rc = sqlite3_prepare(db, "SELECT COUNT(*) AS c FROM article WHERE catid = ?;", -1, &stmt, NULL); sqlite3_bind_text(stmt, 1, sortlevel, -1, SQLITE_STATIC); snprintf(url, sizeof(url), "/panel-article-list-%s", sortlevel); } else { strcpy(url, "/panel-article-list-all"); rc = sqlite3_prepare(db, "SELECT COUNT(*) AS c FROM article;", -1, &stmt, NULL); } rc = sqlite3_step(stmt); if(rc == SQLITE_ROW) { total = sqlite3_column_int(stmt, 0); } sqlite3_finalize(stmt); cur = (page) ? atoi(page) : 1; //分页 pager(tpl, total, conf.page.admin, cur, url, limit); tpl_set_field_int_global(tpl, "total", total); if((sortlevel) && (strcmp(sortlevel, "all") == 0)) { rc = sqlite3_prepare(db, "SELECT art.comment_num, art.hit, art.art_id, art.title, art.content, datetime(art.post_time, 'unixepoch') AS dt, cat.sortname, cat.sortdir FROM article AS art LEFT JOIN category AS cat ON cat.sortdir = art.catid ORDER BY art.art_id DESC LIMIT ?, ?;", -1, &stmt, NULL); sqlite3_bind_int(stmt, 1, limit[0]); sqlite3_bind_int(stmt, 2, limit[1]); } else if((sortlevel) && (is_alpha(sortlevel)) && (strcmp(sortlevel, "html") != 0)) { rc = sqlite3_prepare(db, "SELECT art.comment_num, art.hit, art.art_id, art.title, art.content, datetime(art.post_time, 'unixepoch') AS dt, cat.sortname, cat.sortdir FROM article AS art LEFT JOIN category AS cat ON cat.sortdir = art.catid WHERE art.catid = ? ORDER BY art.art_id DESC LIMIT ?, ?;", -1, &stmt, NULL); sqlite3_bind_text(stmt, 1, sortlevel, -1, SQLITE_STATIC); sqlite3_bind_int(stmt, 2, limit[0]); sqlite3_bind_int(stmt, 3, limit[1]); } else { rc = sqlite3_prepare(db, "SELECT art.comment_num, art.hit, art.art_id, art.title, art.content, datetime(art.post_time, 'unixepoch') AS dt, cat.sortname, cat.sortdir FROM article AS art LEFT JOIN category AS cat ON cat.sortdir = art.catid ORDER BY art.art_id DESC LIMIT ?, ?;", -1, &stmt, NULL); sqlite3_bind_int(stmt, 1, limit[0]); sqlite3_bind_int(stmt, 2, limit[1]); } tpl_select_section(tpl, "data"); while((rc = sqlite3_step(stmt)) == SQLITE_ROW) { for(i=0; i<sqlite3_column_count(stmt); i++) { type = sqlite3_column_type(stmt, i); switch(type) { case SQLITE_INTEGER: tpl_set_field_uint(tpl, sqlite3_column_name(stmt,i), sqlite3_column_int(stmt, i)); break; case SQLITE_TEXT: tpl_set_field(tpl, sqlite3_column_name(stmt,i) ,sqlite3_column_text(stmt,i), strlen(sqlite3_column_text(stmt,i))); break; } } tpl_append_section(tpl); } tpl_deselect_section(tpl); sqlite3_finalize(stmt); html = mspace_malloc(_SERVER->mp, tpl_length(tpl) + 1); tpl_get_content(tpl, html); printf("Content-type: text/html\r\n\r\n%s", html); tpl_free(tpl); mspace_free(_SERVER->mp, html); tpl = NULL; }
void panel_article_insert(struct env_t *_SERVER) { time_t visit; tpl_t *tpl; TCLIST *arr; char *html, path[128], buf[128]; sqlite3_int64 id; sqlite3_stmt *stmt; int rc, i, pos, n, len; const char *title, *sortlevel, *content, *recommend, *keyword, *filename, *file; if(tcmapget2(_SERVER->_POST, "Article_Insert")) { visit = time((time_t*)0); file = tcmapget2(_SERVER->_COOKIE, "file"); title = tcmapget2(_SERVER->_POST, "title"); content = tcmapget2(_SERVER->_POST, "content"); keyword = tcmapget2(_SERVER->_POST, "keyword"); filename = tcmapget2(_SERVER->_POST, "filename"); sortlevel = tcmapget2(_SERVER->_POST, "sortlevel"); recommend = tcmapget2(_SERVER->_POST, "recommend"); pos = strpos(content, "<!-- idx -->"); pos = (pos == -1) ? strlen(content) : pos; rc = sqlite3_prepare(db, "INSERT INTO article(title, content, catid, keyword, filename, recommend, post_time, position, hit, comment_num) VALUES(?, ?, ?, ?, ?, ?, ?, ?, 0, 0);", -1, &stmt, NULL); sqlite3_bind_text(stmt, 1, title, -1, SQLITE_STATIC); sqlite3_bind_text(stmt, 2, content, -1, SQLITE_STATIC); sqlite3_bind_text(stmt, 3, sortlevel, -1, SQLITE_STATIC); sqlite3_bind_text(stmt, 4, keyword, -1, SQLITE_STATIC); sqlite3_bind_text(stmt, 5, filename, -1, SQLITE_STATIC); sqlite3_bind_int(stmt, 6, atoi(recommend)); sqlite3_bind_int(stmt, 7, visit); sqlite3_bind_int(stmt, 8, pos); sqlite3_step(stmt); sqlite3_finalize(stmt); id = sqlite3_last_insert_rowid(db); if(file) { memset(buf, 0, sizeof(buf)); memset(path, 0, sizeof(path)); strftime(buf, sizeof(buf), "%Y/%m/%d", localtime(&visit)); //如果有上传的文件,就把上传的文件,从临时目录移动目标目录 len = snprintf(path, sizeof(path), "./attachment/%s/%llu/", buf, id); if(!is_dir(path)) mkpath(_SERVER->mp, path, 0777); if(!strchr(file, '|')) { memset(buf, 0, sizeof(buf)); snprintf(buf, sizeof(buf), "./files/%s", file); strcat(path, file); // log_debug("panel_article_insert buf = %s\tpath = %s\r\n", buf, path); if(file_exists(buf)) rename(buf, path); } else { arr = explode("|", (char *)file); n = tclistnum(arr); for(i=0; i<n; i++) { memset(buf, 0, sizeof(buf)); filename = tclistval2(arr, i); snprintf(buf, sizeof(buf), "./files/%s", filename); path[len] = '\0'; strcat(path, filename); // log_debug("panel_article_insert explode buf = %s\tpath = %s\r\n", buf, path); if(file_exists(buf)) rename(buf, path); } tclistdel(arr); } path[len] = '\0'; //更新图片附件的 路径 rc = sqlite3_prepare(db, "UPDATE article SET content = replace(content, '/files/', ?) WHERE art_id = ?;", -1, &stmt, NULL); sqlite3_bind_text(stmt, 1, path+1, -1, SQLITE_STATIC); sqlite3_bind_int64(stmt, 2, id); sqlite3_step(stmt); sqlite3_finalize(stmt); } #ifdef _BUILD_HTML memset(path, 0, sizeof(path)); snprintf(path, sizeof(path), "./html/list/%s", sortlevel); ftw(path, fn, 500); #endif make_article_index(_SERVER); printf("Content-type: text/html\r\n\r\n<script>alert('添加成功!');window.location.href='/panel-article-list-%s.html';</script>", sortlevel); return ; } tpl = tpl_alloc(); if (tpl_load(tpl, "./templets/panel/article_insert.html") != TPL_OK) { puts("Content-type: text/html\r\n\r\n./templets/panel/article_insert.html Error loading template file!"); tpl_free(tpl); return ; } rc = sqlite3_prepare(db, "SELECT sortname, sortdir FROM category", -1, &stmt, NULL); tpl_select_section(tpl, "classic"); while((rc = sqlite3_step(stmt)) == SQLITE_ROW) { for(i=0; i<sqlite3_column_count(stmt); i++) { tpl_set_field(tpl, sqlite3_column_name(stmt,i) ,sqlite3_column_text(stmt,i), strlen(sqlite3_column_text(stmt,i))); } tpl_append_section(tpl); } tpl_deselect_section(tpl); sqlite3_finalize(stmt); html = mspace_malloc(_SERVER->mp, tpl_length(tpl) + 1); tpl_get_content(tpl, html); printf("Content-type: text/html\r\n\r\n%s", html); tpl_free(tpl); mspace_free(_SERVER->mp, html); tpl = NULL; }
int main(int argc, char *argv[]) { size_t size1 = 255; void *addr1 = NULL; size_t size2 = 256; void *addr2 = NULL; size_t size3 = kMaxCodeCacheCapacity - 512; void *addr3 = NULL; gExecutableStore = malloc(kMaxCodeCacheCapacity); if (gExecutableStore == NULL) { printf("error malloc\n"); return -1; } gMspace = create_mspace_with_base(gExecutableStore, kMaxCodeCacheCapacity, /*locked=*/ true); printf("-> create mspace\n"); mspace_malloc_stats(gMspace); addr1 = mspace_malloc(gMspace, size1); if (addr1 != NULL) { printf("-> malloc addr1 = %p, 0x%x\n", addr1, (uint32_t)size1); mspace_malloc_stats(gMspace); printf("addr1 size = 0x%x\n", (uint32_t)mspace_usable_size(addr1)); } addr2 = mspace_malloc(gMspace, size2); if (addr2 != NULL) { printf("-> malloc addr2 = %p, 0x%x\n", addr2, (uint32_t)size2); mspace_malloc_stats(gMspace); printf("addr2 size = 0x%x\n", (uint32_t)mspace_usable_size(addr2)); } addr3 = mspace_malloc(gMspace, size3); if (addr3 != NULL) { printf("-> malloc addr3 = %p, 0x%x\n", addr3, (uint32_t)size3); mspace_malloc_stats(gMspace); printf("addr3 size = 0x%x\n", (uint32_t)mspace_usable_size(addr3)); } else { printf("malloc addr3 error!\n"); } if (addr1 != NULL) { mspace_free(gMspace, addr1); } if (addr2 != NULL) { mspace_free(gMspace, addr2); } if (addr3 != NULL) { mspace_free(gMspace, addr3); } printf("-> all free\n"); mspace_malloc_stats(gMspace); _exit: if (gMspace != NULL) { destroy_mspace(gMspace); gMspace = NULL; } if (gExecutableStore != NULL) { free(gExecutableStore); gExecutableStore = NULL; } return 0; }
void sq_vm_free(void *p, SQUnsignedInteger size) { mspace_free(g_mspace, p); }
void extmem_free(void* mem) { extmem_printk("[EXT_MEM] %s addr:0x%x\n", __FUNCTION__, mem); if (extmem_mspace != NULL) mspace_free(extmem_mspace, mem); }
Assembly::~Assembly() { mspace_free(getMspace(), mBase); }
int main(void) { size_t n, m; usCount start, end; size_t roundings[16]; printf("N1527lib test program\n" "-=-=-=-=-=-=-=-=-=-=-\n"); n=mpool_minimum_roundings(roundings, 16); assert(n<16); printf("Minimum roundings from available allocators:\n"); for(m=0; m<n; m++) { printf(" %u\n", roundings[m]); } { mpool pool128, pool4096, poolA; void *temp1, *temp2; size_t usagecount, *alignments, *roundings; pool128=mpool_obtain(alignment128_attributes); temp1=mpool_malloc(pool128, 1); temp2=mpool_malloc(pool128, 1); printf("128 aligned %p (%u), %p (%u)", temp1, mpool_usable_size(pool128, temp1), temp2, mpool_usable_size(pool128, temp2)); assert(!((size_t)temp1 & 127)); assert(!((size_t)temp2 & 127)); mpool_info(pool128, &usagecount, &alignments, &roundings, NULL); printf(" usagecount=%u, roundings[0]=%u\n", usagecount, roundings[0]); pool4096=mpool_obtain(alignment4096_attributes); temp1=mpool_malloc(pool4096, 1); temp2=mpool_malloc(pool4096, 1); printf("4096 aligned %p (%u), %p (%u)", temp1, mpool_usable_size(pool4096, temp1), temp2, mpool_usable_size(pool4096, temp2)); assert(!((size_t)temp1 & 4095)); assert(!((size_t)temp2 & 4095)); mpool_info(pool4096, &usagecount, &alignments, &roundings, NULL); printf(" usagecount=%u, roundings[0]=%u\n", usagecount, roundings[0]); poolA=mpool_obtain(alignment128_attributes_a); mpool_info(poolA, &usagecount, &alignments, &roundings, NULL); printf("PoolA: usagecount=%u, roundings[0]=%u\n", usagecount, roundings[0]); assert(poolA==pool128); } syspool=mpool_obtain(MPOOL_DEFAULT); srand(1); for(n=0; n<RECORDS; n++) sizes[n]=rand() & 1023; if(1) { unsigned frees=0; printf("Fragmenting free space ...\n"); start=GetUsCount(); while(GetUsCount()-start<3000000000000) { n=rand() % RECORDS; if(ptrs[n]) { mpool_free(syspool, ptrs[n]); ptrs[n]=0; frees++; } else ptrs[n]=mpool_malloc(syspool, sizes[n]); } memset(ptrs, 0, RECORDS*sizeof(void *)); printf("Did %u frees\n", frees); } if(0) { typedef void* mspace; extern mspace get_dlmalloc_mspace(mpool pool); extern void* mspace_malloc(mspace msp, size_t bytes); extern void mspace_free(mspace msp, void* mem); printf("\ndlmalloc Speed test\n" "-------------------\n"); { mspace ms=get_dlmalloc_mspace(syspool); for(m=0; m<3; m++) { size_t count=RECORDS, size=1024; for(n=0; n<RECORDS; n++) sizes[n]=rand() & 1023; start=GetUsCount(); for(n=0; n<RECORDS; n++) { ptrs[n]=mspace_malloc(ms, sizes[n]); } end=GetUsCount(); printf("mspace_malloc() does %f mallocs/sec\n", RECORDS/((end-start)/1000000000000.0)); start=GetUsCount(); for(n=0; n<RECORDS; n++) { mspace_free(ms, ptrs[n]); ptrs[n]=0; } end=GetUsCount(); printf("mspace_free() does %f frees/sec\n\n", RECORDS/((end-start)/1000000000000.0)); } } } if(1) { printf("\nMPool Speed test\n" "----------------\n"); for(m=0; m<3; m++) { size_t count=RECORDS, size=1024; for(n=0; n<RECORDS; n++) sizes[n]=rand() & 1023; start=GetUsCount(); mpool_batch(syspool, NULL, ptrs, sizes, &count, 0); end=GetUsCount(); printf("mpool_batch() does %f mallocs/sec\n", RECORDS/((end-start)/1000000000000.0)); count=RECORDS; start=GetUsCount(); mpool_batch(syspool, NULL, ptrs, NULL, &count, 0); end=GetUsCount(); printf("mpool_batch() does %f frees/sec\n", RECORDS/((end-start)/1000000000000.0)); start=GetUsCount(); for(n=0; n<RECORDS; n++) { ptrs[n]=mpool_malloc(syspool, sizes[n]); } end=GetUsCount(); printf("mpool_malloc() does %f mallocs/sec\n", RECORDS/((end-start)/1000000000000.0)); start=GetUsCount(); for(n=0; n<RECORDS; n++) { mpool_free(syspool, ptrs[n]); ptrs[n]=0; } end=GetUsCount(); printf("mpool_free() does %f frees/sec\n\n", RECORDS/((end-start)/1000000000000.0)); } } #ifdef _MSC_VER printf("Press Return to exit ...\n"); getchar(); #endif return 0; }
void make_article_index(struct env_t *_SERVER) { uint total; tpl_t *tpl; char *html; FILE *fp; int rc, i, type; uint limit[2]; sqlite3_stmt *stmt; tpl = tpl_alloc(); if (tpl_load(tpl, "./templets/blog/article_list.html") != TPL_OK) { puts("Content-type: text/html\r\n\r\n./templets/blog/article_list.html Error loading template file!"); tpl_free(tpl); return ; } //获取总数据集大小 total = dataset_count("SELECT COUNT(*) AS c FROM article"); tpl_set_field_int_global(tpl, "total", total); //分页 pager(tpl, total, conf.page.blog, 1, "/article-list-all", limit); //获取分类数据 assign_tpl_category(tpl); //加载推荐数据 assign_tpl_recommend(tpl); //加载友情链接 assign_tpl_friendlink(tpl); rc = sqlite3_prepare(db, "SELECT art.filename, art.comment_num, art.hit, art.art_id, art.title, substring(art.content, art.position) AS c, datetime(art.post_time, 'unixepoch') AS dt, cat.sortname, cat.sortdir FROM article AS art LEFT JOIN category AS cat ON cat.sortdir = art.catid ORDER BY art.art_id DESC LIMIT ?;", -1, &stmt, NULL); sqlite3_bind_int(stmt, 1, conf.page.blog); tpl_select_section(tpl, "data"); while((rc = sqlite3_step(stmt)) == SQLITE_ROW) { for(i=0; i<sqlite3_column_count(stmt); i++) { type = sqlite3_column_type(stmt, i); switch(type) { case SQLITE_INTEGER: tpl_set_field_uint(tpl, sqlite3_column_name(stmt,i), sqlite3_column_int(stmt, i)); break; case SQLITE_TEXT: tpl_set_field(tpl, sqlite3_column_name(stmt,i) ,sqlite3_column_text(stmt,i), strlen(sqlite3_column_text(stmt,i))); break; } } tpl_append_section(tpl); } tpl_deselect_section(tpl); sqlite3_finalize(stmt); html = mspace_malloc(_SERVER->mp, tpl_length(tpl) + 1); tpl_get_content(tpl, html); //printf("Content-type: text/html\r\n\r\n%s", html); fp = fopen("./index.htm", "wb"); if(fp) { fwrite(html, sizeof(char), tpl_length(tpl), fp); fclose(fp); } chmod("./index.htm", S_IRWXU | S_IRWXG | S_IRWXO); tpl_free(tpl); mspace_free(_SERVER->mp, html); tpl = NULL; }