/** * Initializes the various objects of ::cpl structure. */ void objects_init(void) { cpl.ob = mempool_get(pool_object); cpl.below = mempool_get(pool_object); cpl.below->weight = -111; }
char * slapi_ch_realloc( char *block, unsigned long size ) { char *newmem; unsigned long lsize; unsigned long origsize; char *realblock; char *realnewmem; if ( block == NULL ) { return( slapi_ch_malloc( size ) ); } if (size <= 0) { log_negative_alloc_msg( "realloc", "bytes", size ); return block; } lsize = size + sizeof(unsigned long); if (lsize <= 1024) { newmem = slapi_ch_realloc_core( block, lsize ); } else if (lsize <= 67108864) { /* return 2KB ~ 64MB memory to memory pool */ unsigned long roundup = 1; int n = 0; while (1) { roundup <<= 1; n++; if (roundup >= lsize) { break; } } PR_ASSERT(n >= 11 && n <= 26); newmem = (char *)mempool_get(n-11); /* 11: 2^11 = 2K */ if (NULL == newmem) { newmem = slapi_ch_realloc_core( block, roundup ); } else { realblock = block - sizeof(unsigned long); origsize = *(unsigned long *)realblock - sizeof(unsigned long);; memcpy(newmem, block, origsize); slapi_ch_free_string(&block); } } else { realblock = block - sizeof(unsigned long); origsize = *(unsigned long *)realblock - sizeof(unsigned long);; newmem = slapi_ch_mmap( size ); memcpy(newmem, block, origsize); realnewmem = newmem - sizeof(unsigned long); *(unsigned long *)realnewmem = lsize; slapi_ch_free_string(&block); } if(!counters_created) { create_counters(); counters_created= 1; } PR_INCREMENT_COUNTER(slapi_ch_counter_realloc); return( newmem ); }
/** * Create a process specific file. */ static int create_proc_file(vnode_t * pdir, pid_t pid, const char * filename, enum procfs_filetype ftype) { vnode_t * vn; struct procfs_info * spec; int err; KASSERT(pdir != NULL, "pdir must be set"); spec = mempool_get(specinfo_pool); if (!spec) return -ENOMEM; /* Create a specinfo */ spec->ftype = ftype; spec->pid = pid; err = pdir->vnode_ops->mknod(pdir, filename, S_IFREG | PROCFS_PERMS, spec, &vn); if (err) { mempool_return(specinfo_pool, spec); return -ENOTDIR; } spec->vnode = vn; vn->vn_specinfo = spec; vn->vnode_ops = &procfs_vnode_ops; vrele(vn); return 0; }
RSTokenizer *GetSimpleTokenizer(Stemmer *stemmer, StopWordList *stopwords) { if (!tokpoolLatin_g) { tokpoolLatin_g = mempool_new(16, newLatinTokenizerAlloc, tokenizerFree); } RSTokenizer *t = mempool_get(tokpoolLatin_g); t->Reset(t, stemmer, stopwords, 0); return t; }
char * slapi_ch_calloc( unsigned long nelem, unsigned long size ) { char *newmem; unsigned long lsize; if (size <= 0) { log_negative_alloc_msg( "calloc", "bytes", size ); return 0; } if (nelem <= 0) { log_negative_alloc_msg( "calloc", "elements", nelem ); return 0; } lsize = nelem * size + sizeof(unsigned long); if (lsize <= 1024) { newmem = slapi_ch_calloc_core( lsize ); } else if (lsize <= 67108864) { /* return 2KB ~ 64MB memory to memory pool */ unsigned long roundup = 1; int n = 0; while (1) { roundup <<= 1; n++; if (roundup >= lsize) { break; } } PR_ASSERT(n >= 11 && n <= 26); newmem = (char *)mempool_get(n-11); /* 11: 2^11 = 2K */ if (NULL == newmem) { newmem = slapi_ch_calloc_core( roundup ); } else { memset (newmem, 0, size * nelem); } } else { unsigned long mysize = size * nelem; newmem = slapi_ch_mmap( mysize ); memset(newmem, 0, mysize); } if(!counters_created) { create_counters(); counters_created= 1; } PR_INCREMENT_COUNTER(slapi_ch_counter_calloc); PR_INCREMENT_COUNTER(slapi_ch_counter_created); PR_INCREMENT_COUNTER(slapi_ch_counter_exist); return( newmem ); }
/** * Creates a new object and inserts it into 'env'. * @param env * Which object to insert the created object into. Can be NULL * not to insert the created object anywhere. * @param tag * The object's ID. * @param bflag * If 1, the object will be added to the end of the * inventory instead of the start. * @return * The created object. */ object *object_create(object *env, tag_t tag, int bflag) { object *op = mempool_get(pool_object); op->tag = tag; if (env != NULL) { object_add(env, op, bflag); } object_redraw(op); return op; }
/** * Clone old process descriptor. */ static struct proc_info * clone_proc_info(struct proc_info * const old_proc) { struct proc_info * new_proc; KERROR_DBG("clone proc info of pid %u\n", old_proc->pid); new_proc = mempool_get(proc_pool); if (!new_proc) { return NULL; } memcpy(new_proc, old_proc, sizeof(struct proc_info)); return new_proc; }
int main(int argc, char **argv) { #ifdef DEBUG logfile_create("test.log", 3); #endif char *conffile = "memlink.conf"; MemPool *mp; MemItem *item_100; MemItem *item_10; myconfig_create(conffile); my_runtime_create_common("memlink"); mp = g_runtime->mpool; item_100 = (MemItem *)zz_malloc(sizeof(MemItem)); memset(item_100, 0x0, sizeof(MemItem)); item_10 = (MemItem *)zz_malloc(sizeof(MemItem)); memset(item_10, 0x0, sizeof(MemItem)); DataBlock *dbk; int i; for (i = 0; i < 100; i++) { dbk = mempool_get(mp, 100 + sizeof(DataBlock)); dbk->next = item_100->data; item_100->data = dbk; } for (i = 0; i < mp->used; i++) { if (mp->freemem[i].memsize == 100 + sizeof(DataBlock)) { break; } } if (i == mp->used) { DERROR("i == mp->used\n"); return -1; } if (mp->freemem[i].total != 100) { DERROR("mp->freemem[i].total != 100\n"); return -1; } if (mp->freemem[i].block_count != 0) { DERROR("mp->freemem[i].block_count != 0\n"); return -1; } return 0; }