/** * Free an element from the mempool. * * \note doesnt protect against double frees, don't be stupid! */ void BLI_mempool_free(BLI_mempool *pool, void *addr) { BLI_freenode *newhead = addr; if (pool->flag & BLI_MEMPOOL_ALLOW_ITER) { #ifndef NDEBUG /* this will detect double free's */ BLI_assert(newhead->freeword != FREEWORD); #endif newhead->freeword = FREEWORD; } newhead->next = pool->free; pool->free = newhead; pool->totused--; /* nothing is in use; free all the chunks except the first */ if (pool->totused == 0) { BLI_freenode *curnode = NULL; char *tmpaddr = NULL; int i; BLI_mempool_chunk *mpchunk = NULL; BLI_mempool_chunk *first = pool->chunks.first; BLI_remlink(&pool->chunks, first); if (pool->flag & BLI_MEMPOOL_SYSMALLOC) { for (mpchunk = pool->chunks.first; mpchunk; mpchunk = mpchunk->next) { free(mpchunk->data); } BLI_freelist(&(pool->chunks)); } else { for (mpchunk = pool->chunks.first; mpchunk; mpchunk = mpchunk->next) { MEM_freeN(mpchunk->data); } BLI_freelistN(&(pool->chunks)); } BLI_addtail(&pool->chunks, first); #ifdef USE_TOTALLOC pool->totalloc = pool->pchunk; #endif pool->free = first->data; /* start of the list */ for (tmpaddr = first->data, i = 0; i < pool->pchunk; i++) { curnode = ((BLI_freenode *)tmpaddr); tmpaddr += pool->esize; curnode->next = (BLI_freenode *)tmpaddr; } curnode->next = NULL; /* terminate the list */ } }
void BLI_mempool_destroy(BLI_mempool *pool) { BLI_mempool_chunk *mpchunk=NULL; if(pool->use_sysmalloc) { for(mpchunk = pool->chunks.first; mpchunk; mpchunk = mpchunk->next) { free(mpchunk->data); } BLI_freelist(&(pool->chunks)); free(pool); } else { for(mpchunk = pool->chunks.first; mpchunk; mpchunk = mpchunk->next) { MEM_freeN(mpchunk->data); } BLI_freelistN(&(pool->chunks)); MEM_freeN(pool); } }
/** * Free the mempool its self (and all elements). */ void BLI_mempool_destroy(BLI_mempool *pool) { BLI_mempool_chunk *mpchunk = NULL; if (pool->flag & BLI_MEMPOOL_SYSMALLOC) { for (mpchunk = pool->chunks.first; mpchunk; mpchunk = mpchunk->next) { free(mpchunk->data); } BLI_freelist(&(pool->chunks)); free(pool); } else { for (mpchunk = pool->chunks.first; mpchunk; mpchunk = mpchunk->next) { MEM_freeN(mpchunk->data); } BLI_freelistN(&(pool->chunks)); MEM_freeN(pool); } }
void BLI_mempool_free(BLI_mempool *pool, void *addr){ //doesnt protect against double frees, dont be stupid! BLI_freenode *newhead = addr; BLI_freenode *curnode=NULL; char *tmpaddr=NULL; int i; newhead->next = pool->free; pool->free = newhead; pool->totused--; /*nothing is in use; free all the chunks except the first*/ if (pool->totused == 0) { BLI_mempool_chunk *mpchunk=NULL, *first; first = pool->chunks.first; BLI_remlink(&pool->chunks, first); for(mpchunk = pool->chunks.first; mpchunk; mpchunk = mpchunk->next) { if(pool->use_sysmalloc) free(mpchunk->data); else MEM_freeN(mpchunk->data); } pool->use_sysmalloc ? BLI_freelist(&(pool->chunks)) : BLI_freelistN(&(pool->chunks)); BLI_addtail(&pool->chunks, first); pool->totalloc = pool->pchunk; pool->free = first->data; /*start of the list*/ for(tmpaddr = first->data, i=0; i < pool->pchunk; i++){ curnode = ((BLI_freenode*)tmpaddr); tmpaddr += pool->esize; curnode->next = (BLI_freenode*)tmpaddr; } curnode->next = NULL; /*terminate the list*/ } }
static void bli_builddir(const char *dirname, const char *relname) { struct dirent *fname; struct dirlink *dlink; int rellen, newnum = 0; char buf[256]; DIR *dir; BLI_strncpy(buf, relname, sizeof(buf)); rellen=strlen(relname); if (rellen) { buf[rellen]='/'; rellen++; } #ifndef WIN32 if (chdir(dirname) == -1) { perror(dirname); return; } #else UTF16_ENCODE(dirname); if (!SetCurrentDirectoryW(dirname_16)) { perror(dirname); free(dirname_16); return; } UTF16_UN_ENCODE(dirname); #endif if ((dir = (DIR *)opendir("."))) { while ((fname = (struct dirent*) readdir(dir)) != NULL) { dlink = (struct dirlink *)malloc(sizeof(struct dirlink)); if (dlink) { BLI_strncpy(buf + rellen, fname->d_name, sizeof(buf) - rellen); dlink->name = BLI_strdup(buf); BLI_addhead(dirbase, dlink); newnum++; } } if (newnum) { if (files) { void *tmp = realloc(files, (totnum+newnum) * sizeof(struct direntry)); if (tmp) { files = (struct direntry *)tmp; } else { /* realloc fail */ free(files); files = NULL; } } if (files==NULL) files=(struct direntry *)malloc(newnum * sizeof(struct direntry)); if (files) { dlink = (struct dirlink *) dirbase->first; while (dlink) { memset(&files[actnum], 0, sizeof(struct direntry)); files[actnum].relname = dlink->name; files[actnum].path = BLI_strdupcat(dirname, dlink->name); // use 64 bit file size, only needed for WIN32 and WIN64. // Excluding other than current MSVC compiler until able to test #ifdef WIN32 {wchar_t * name_16 = alloc_utf16_from_8(dlink->name, 0); #if (defined(WIN32) || defined(WIN64)) && (_MSC_VER>=1500) _wstat64(name_16, &files[actnum].s); #elif defined(__MINGW32__) _stati64(dlink->name, &files[actnum].s); #endif free(name_16);}; #else stat(dlink->name, &files[actnum].s); #endif files[actnum].type=files[actnum].s.st_mode; files[actnum].flags = 0; totnum++; actnum++; dlink = dlink->next; } } else { printf("Couldn't get memory for dir\n"); exit(1); } BLI_freelist(dirbase); if (files) qsort(files, actnum, sizeof(struct direntry), (int (*)(const void *, const void*))bli_compare); } else { printf("%s empty directory\n", dirname); } closedir(dir); } else { printf("%s non-existant directory\n", dirname); } }
/** * Scans the directory named *dirname and appends entries for its contents to files. */ static void bli_builddir(struct BuildDirCtx *dir_ctx, const char *dirname) { struct ListBase dirbase = {NULL, NULL}; int newnum = 0; DIR *dir; if ((dir = opendir(dirname)) != NULL) { const struct dirent *fname; while ((fname = readdir(dir)) != NULL) { struct dirlink * const dlink = (struct dirlink *)malloc(sizeof(struct dirlink)); if (dlink != NULL) { dlink->name = BLI_strdup(fname->d_name); BLI_addhead(&dirbase, dlink); newnum++; } } if (newnum) { if (dir_ctx->files) { void * const tmp = realloc(dir_ctx->files, (dir_ctx->nrfiles + newnum) * sizeof(struct direntry)); if (tmp) { dir_ctx->files = (struct direntry *)tmp; } else { /* realloc fail */ free(dir_ctx->files); dir_ctx->files = NULL; } } if (dir_ctx->files == NULL) dir_ctx->files = (struct direntry *)malloc(newnum * sizeof(struct direntry)); if (dir_ctx->files) { struct dirlink * dlink = (struct dirlink *) dirbase.first; struct direntry *file = &dir_ctx->files[dir_ctx->nrfiles]; while (dlink) { char fullname[PATH_MAX]; memset(file, 0, sizeof(struct direntry)); file->relname = dlink->name; file->path = BLI_strdupcat(dirname, dlink->name); BLI_join_dirfile(fullname, sizeof(fullname), dirname, dlink->name); // use 64 bit file size, only needed for WIN32 and WIN64. // Excluding other than current MSVC compiler until able to test #ifdef WIN32 { wchar_t *name_16 = alloc_utf16_from_8(fullname, 0); #if defined(_MSC_VER) && (_MSC_VER >= 1500) _wstat64(name_16, &file->s); #elif defined(__MINGW32__) _stati64(fullname, &file->s); #endif free(name_16); } #else stat(fullname, &file->s); #endif file->type = file->s.st_mode; file->flags = 0; dir_ctx->nrfiles++; file++; dlink = dlink->next; } } else { printf("Couldn't get memory for dir\n"); exit(1); } BLI_freelist(&dirbase); if (dir_ctx->files) { qsort(dir_ctx->files, dir_ctx->nrfiles, sizeof(struct direntry), (int (*)(const void *, const void *))bli_compare); } } else { printf("%s empty directory\n", dirname); } closedir(dir); } else { printf("%s non-existant directory\n", dirname); } }
/** * Scans the directory named *dirname and appends entries for its contents to files. */ static void bli_builddir(struct BuildDirCtx *dir_ctx, const char *dirname) { struct ListBase dirbase = {NULL, NULL}; int newnum = 0; DIR *dir; if ((dir = opendir(dirname)) != NULL) { const struct dirent *fname; while ((fname = readdir(dir)) != NULL) { struct dirlink * const dlink = (struct dirlink *)malloc(sizeof(struct dirlink)); if (dlink != NULL) { dlink->name = BLI_strdup(fname->d_name); BLI_addhead(&dirbase, dlink); newnum++; } } if (newnum) { if (dir_ctx->files) { void * const tmp = MEM_reallocN(dir_ctx->files, (dir_ctx->nrfiles + newnum) * sizeof(struct direntry)); if (tmp) { dir_ctx->files = (struct direntry *)tmp; } else { /* realloc fail */ MEM_freeN(dir_ctx->files); dir_ctx->files = NULL; } } if (dir_ctx->files == NULL) dir_ctx->files = (struct direntry *)MEM_mallocN(newnum * sizeof(struct direntry), __func__); if (dir_ctx->files) { struct dirlink * dlink = (struct dirlink *) dirbase.first; struct direntry *file = &dir_ctx->files[dir_ctx->nrfiles]; while (dlink) { char fullname[PATH_MAX]; memset(file, 0, sizeof(struct direntry)); file->relname = dlink->name; file->path = BLI_strdupcat(dirname, dlink->name); BLI_join_dirfile(fullname, sizeof(fullname), dirname, dlink->name); if (BLI_stat(fullname, &file->s) != -1) { file->type = file->s.st_mode; } file->flags = 0; dir_ctx->nrfiles++; file++; dlink = dlink->next; } } else { printf("Couldn't get memory for dir\n"); exit(1); } BLI_freelist(&dirbase); if (dir_ctx->files) { qsort(dir_ctx->files, dir_ctx->nrfiles, sizeof(struct direntry), (int (*)(const void *, const void *))bli_compare); } } else { printf("%s empty directory\n", dirname); } closedir(dir); } else { printf("%s non-existent directory\n", dirname); } }