Diff for /libelwix/src/mem.c between versions 1.1 and 1.5

version 1.1, 2013/01/17 10:05:35 version 1.5, 2015/06/25 17:53:50
Line 12  terms: Line 12  terms:
 All of the documentation and software included in the ELWIX and AITNET  All of the documentation and software included in the ELWIX and AITNET
 Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>  Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   
Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013Copyright 2004 - 2015
         by Michael Pounov <misho@elwix.org>.  All rights reserved.          by Michael Pounov <misho@elwix.org>.  All rights reserved.
   
 Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
Line 218  mpool_malloc(mpool_t * __restrict mp, u_int size, cons Line 218  mpool_malloc(mpool_t * __restrict mp, u_int size, cons
                         return NULL;                          return NULL;
                 } else {        /* quota */                  } else {        /* quota */
                         mp->pool_quota.curr += size;                          mp->pool_quota.curr += size;
                           mp->pool_quota.real += 1 << (idx + MEM_MIN_BUCKET);
                         memset(m->alloc_mem, 0, align + 12);                          memset(m->alloc_mem, 0, align + 12);
                 }                  }
         }          }
Line 326  mpool_realloc(mpool_t * __restrict mp, void * __restri Line 327  mpool_realloc(mpool_t * __restrict mp, void * __restri
         }          }
         /* quota */          /* quota */
         mp->pool_quota.curr += (u_long) newsize - osize;          mp->pool_quota.curr += (u_long) newsize - osize;
           mp->pool_quota.real += (1 << (idx + MEM_MIN_BUCKET)) - (1 << (oidx + MEM_MIN_BUCKET));
   
         m->alloc_mem[0] = newsize / sizeof(u_int);          m->alloc_mem[0] = newsize / sizeof(u_int);
         m->alloc_mem[1] = MEM_MAGIC_START;          m->alloc_mem[1] = MEM_MAGIC_START;
Line 382  mpool_purge(mpool_t * __restrict mp, u_int atmost) Line 384  mpool_purge(mpool_t * __restrict mp, u_int atmost)
                         mp->pool_bytes.free += mem_size(m);                          mp->pool_bytes.free += mem_size(m);
                         /* quota */                          /* quota */
                         mp->pool_quota.curr -= mem_size(m);                          mp->pool_quota.curr -= mem_size(m);
                           mp->pool_quota.real -= 1 << (i + MEM_MIN_BUCKET);
   
                         if (m->alloc_mem)                          if (m->alloc_mem)
                                 free(m->alloc_mem);                                  free(m->alloc_mem);
Line 407  mpool_free(mpool_t * __restrict mp, void * __restrict  Line 410  mpool_free(mpool_t * __restrict mp, void * __restrict 
         int idx;          int idx;
         struct tagAlloc *m, *tmp;          struct tagAlloc *m, *tmp;
   
        assert(data);        if (!data)
                 return 0;
         if (!mp) {          if (!mp) {
                 elwix_SetErr(EINVAL, "Pool not specified");                  elwix_SetErr(EINVAL, "Pool not specified");
                 return -1;                  return -1;
Line 439  mpool_free(mpool_t * __restrict mp, void * __restrict  Line 443  mpool_free(mpool_t * __restrict mp, void * __restrict 
                                 mp->pool_bytes.free += mem_size(m);                                  mp->pool_bytes.free += mem_size(m);
                                 /* quota */                                  /* quota */
                                 mp->pool_quota.curr -= mem_size(m);                                  mp->pool_quota.curr -= mem_size(m);
                                   mp->pool_quota.real -= 1 << (idx + MEM_MIN_BUCKET);
   
                                 if (m->alloc_mem)                                  if (m->alloc_mem)
                                         free(m->alloc_mem);                                          free(m->alloc_mem);
Line 491  mpool_free2(mpool_t * __restrict mp, u_int size, const Line 496  mpool_free2(mpool_t * __restrict mp, u_int size, const
                                 mp->pool_bytes.free += mem_size(m);                                  mp->pool_bytes.free += mem_size(m);
                                 /* quota */                                  /* quota */
                                 mp->pool_quota.curr -= mem_size(m);                                  mp->pool_quota.curr -= mem_size(m);
                                   mp->pool_quota.real -= 1 << (idx + MEM_MIN_BUCKET);
   
                                 if (m->alloc_mem)                                  if (m->alloc_mem)
                                         free(m->alloc_mem);                                          free(m->alloc_mem);
Line 544  mpool_strdup(mpool_t * __restrict mp, const char *str, Line 550  mpool_strdup(mpool_t * __restrict mp, const char *str,
  * @memname = Memory name   * @memname = Memory name
  * return: NULL error or not found and !=NULL allocated memory    * return: NULL error or not found and !=NULL allocated memory 
  */   */
inline struct tagAlloc *struct tagAlloc *
 mpool_getmembynam(mpool_t * __restrict mp, u_int size, const char *memname)  mpool_getmembynam(mpool_t * __restrict mp, u_int size, const char *memname)
 {  {
         int idx;          int idx;
Line 567  mpool_getmembynam(mpool_t * __restrict mp, u_int size, Line 573  mpool_getmembynam(mpool_t * __restrict mp, u_int size,
  * @addr = allocated memory from mpool_malloc()   * @addr = allocated memory from mpool_malloc()
  * return: usable size of allocated memory block   * return: usable size of allocated memory block
  */   */
inline u_intu_int
 mpool_getsizebyaddr(void * __restrict data)  mpool_getsizebyaddr(void * __restrict data)
 {  {
         if (mpool_chkaddr(data))          if (mpool_chkaddr(data))
Line 582  mpool_getsizebyaddr(void * __restrict data) Line 588  mpool_getsizebyaddr(void * __restrict data)
  * @data = allocated memory from mpool_malloc()   * @data = allocated memory from mpool_malloc()
  * return: -1 bad address, 1 corrupted address or 0 ok   * return: -1 bad address, 1 corrupted address or 0 ok
  */   */
inline intint
 mpool_chkaddr(void * __restrict data)  mpool_chkaddr(void * __restrict data)
 {  {
         /* check address range */          /* check address range */
Line 602  mpool_chkaddr(void * __restrict data) Line 608  mpool_chkaddr(void * __restrict data)
  * @maxmem = New max quota size   * @maxmem = New max quota size
  * return: old maximum memory quota size   * return: old maximum memory quota size
  */   */
inline u_longu_long
 mpool_setquota(mpool_t * __restrict mp, u_long maxmem)  mpool_setquota(mpool_t * __restrict mp, u_long maxmem)
 {  {
         u_long ret;          u_long ret;
Line 626  mpool_setquota(mpool_t * __restrict mp, u_long maxmem) Line 632  mpool_setquota(mpool_t * __restrict mp, u_long maxmem)
  * mpool_getquota() - Get memory quota   * mpool_getquota() - Get memory quota
  *   *
  * @mp = Memory pool   * @mp = Memory pool
 * @currmem = Return current memory * @currmem = Return current memory usage
  * @realmem = Return current real memory usage
  * @maxmem = Return max quota size   * @maxmem = Return max quota size
  * return: none   * return: none
  */   */
inline voidvoid
mpool_getquota(mpool_t * __restrict mp, u_long *currmem, u_long *maxmem)mpool_getquota(mpool_t * __restrict mp, u_long *currmem, u_long *realmem, u_long *maxmem)
 {  {
         if (!mp)          if (!mp)
                 return;                  return;
   
         if (maxmem)          if (maxmem)
                 *maxmem = mp->pool_quota.max;                  *maxmem = mp->pool_quota.max;
           if (realmem)
                   *realmem = mp->pool_quota.real;
         if (currmem)          if (currmem)
                 *currmem = mp->pool_quota.curr;                  *currmem = mp->pool_quota.curr;
 }  }
Line 733  mpool_xstrdup(const char *str) Line 742  mpool_xstrdup(const char *str)
 {  {
         return mpool_strdup(elwix_mpool, str, elwix_Prog);          return mpool_strdup(elwix_mpool, str, elwix_Prog);
 }  }
   
   /*
    * mpool_xstatistics() - elwix memory pool statistics wrapper
    *
    * @cb = Export statistics to callback
    * return: none
    */
   void
   mpool_xstatistics(mpool_stat_cb cb)
   {
           mpool_statistics(elwix_mpool, cb);
   }
   
   static void
   xdump_show(u_int size, u_int act, u_int inact)
   {
           if (!act && !inact)
                   return; /* skip empty bucket */
   
           if (size < 1024)
                   printf("\t\t* BUCKET %uB size, %u active, %u inactive\n", 
                                   size, act, inact);
           else if (size < 1048576)
                   printf("\t\t* BUCKET %uKB size, %u active, %u inactive\n", 
                                   size / 1024, act, inact);
           else
                   printf("\t\t* BUCKET %uMB size, %u active, %u inactive\n", 
                                   size / 1048576, act, inact);
   }
   
   /*
    * mpool_xdump() - Dump elwix memory pool statistics
    *
    * @fmt = format string
    * @... = argument(s)
    * return: none
    */
   void
   mpool_xdump(const char *fmt, ...)
   {
           va_list lst;
   
           if (fmt) {
                   va_start(lst, fmt);
                   vprintf(fmt, lst);
                   va_end(lst);
           } else
                   printf("\n%s(%d)\n", __func__, __LINE__);
   
           printf("------------------------------------------------------------\n");
           printf( " ELWIX memory pool ::\n"
                   "\t- quotas Current/Real/Max = %lu/%lu/%lu\n"
                   "\t- calls Alloc/Free/Cache = %lu/%lu/%lu\n"
                   "\t- bytes Alloc/Free/Cache = %lu/%lu/%lu\n"
                   "\t- buckets :\n", 
                   elwix_mpool->pool_quota.curr, elwix_mpool->pool_quota.real, elwix_mpool->pool_quota.max, 
                   elwix_mpool->pool_calls.alloc, elwix_mpool->pool_calls.free, 
                   elwix_mpool->pool_calls.cache, elwix_mpool->pool_bytes.alloc, 
                   elwix_mpool->pool_bytes.free, elwix_mpool->pool_bytes.cache);
   
           mpool_statistics(elwix_mpool, xdump_show);
           printf("------------------------------------------------------------\n");
   }
   

Removed from v.1.1  
changed lines
  Added in v.1.5


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>