Diff for /libelwix/src/mem.c between versions 1.5.2.1 and 1.8.56.1

version 1.5.2.1, 2015/07/01 21:31:26 version 1.8.56.1, 2022/10/23 23:46:01
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 - 2015Copyright 2004 - 2022
         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 250  mpool_realloc(mpool_t * __restrict mp, void * __restri Line 250  mpool_realloc(mpool_t * __restrict mp, void * __restri
         int idx, oidx;          int idx, oidx;
         void *p;          void *p;
         u_int osize;          u_int osize;
 #ifdef MPOOL_USE_REALLOC  
         struct tagAlloc *m, *tmp;  
         u_int align;  
 #endif  
   
         /* if !data execute mpool_malloc() */          /* if !data execute mpool_malloc() */
         if (!data)          if (!data)
Line 289  mpool_realloc(mpool_t * __restrict mp, void * __restri Line 285  mpool_realloc(mpool_t * __restrict mp, void * __restri
                 return NULL;                  return NULL;
         }          }
   
 #ifndef MPOOL_USE_REALLOC  
         if (oidx != idx) {          if (oidx != idx) {
                 mpool_unlock(mp);                  mpool_unlock(mp);
                 p = mpool_malloc(mp, newsize, memname);                  p = mpool_malloc(mp, newsize, memname);
                 if (!p)                  if (!p)
                         return NULL;                          return NULL;
   
                memcpy(p, data, osize);                memcpy(p, data, MIN(osize, newsize));
                 mpool_free(mp, data, 0);                  mpool_free(mp, data, 0);
                   data = p;
         } else {          } else {
                 p = data;                  p = data;
   
Line 311  mpool_realloc(mpool_t * __restrict mp, void * __restri Line 307  mpool_realloc(mpool_t * __restrict mp, void * __restri
         }          }
   
         return p;          return p;
 #else  
         /* find old memory block */  
         TAILQ_FOREACH_SAFE(m, &mp->pool_active[oidx], alloc_node, tmp) {  
                 if (mem_data(m, void*) == data && mem_size(m) == osize) {  
                         /* case in different buckets */  
                         if (oidx != idx) {  
                                 TAILQ_REMOVE(&mp->pool_active[oidx], m, alloc_node);  
                                 /* statistics */  
                                 mp->pool_calls.alloc--;  
                         }  
                         mp->pool_bytes.alloc -= osize;  
                         break;  
                 }  
         }  
         /* memory block not found! */  
         if (!m) {  
                 mpool_unlock(mp);  
                 elwix_SetErr(EFAULT, "Memory block not found");  
                 return NULL;  
         }  
   
         /* try to reallocate memory block to new bucket */  
         if (oidx != idx) {  
                 align = 1 << (idx + MEM_MIN_BUCKET);  
                 p = realloc(m->alloc_mem, align + 12);  
                 if (!p) {  
                         LOGERR;  
   
                         /* restore to old bucket pulled memory block for reallocation */  
                         TAILQ_INSERT_HEAD(&mp->pool_active[oidx], m, alloc_node);  
                         /* statistics */  
                         mp->pool_calls.alloc++;  
                         mp->pool_bytes.alloc += osize;  
   
                         mpool_unlock(mp);  
                         return NULL;  
                 } else  
                         m->alloc_mem = (u_int*) p;  
         }  
         /* quota */  
         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[1] = MEM_MAGIC_START;  
         m->alloc_mem[2 + newsize / sizeof(u_int)] = MEM_MAGIC_STOP;  
   
         if (oidx != idx) {  
                 TAILQ_INSERT_HEAD(&mp->pool_active[idx], m, alloc_node);  
                 /* statistics */  
                 mp->pool_calls.alloc++;  
         }  
         mp->pool_bytes.alloc += newsize;  
   
         if (memname)  
                 strlcpy(m->alloc_name, memname, sizeof m->alloc_name);  
   
         mpool_unlock(mp);  
         return mem_data(m, void*);  
 #endif  
 }  }
   
 /*  /*
Line 684  mpool_getquota(mpool_t * __restrict mp, u_long *currme Line 620  mpool_getquota(mpool_t * __restrict mp, u_long *currme
  *   *
  * @mp = Memory pool   * @mp = Memory pool
  * @cb = Export statistics to callback   * @cb = Export statistics to callback
 * return: none * return: -1 error or >0 bytes in data buffer
  */   */
voidint
mpool_statistics(mpool_t * __restrict mp, mpool_stat_cb cb)mpool_statistics(mpool_t * __restrict mp, mpool_stat_cb cb, void *data, u_int datlen)
 {  {
         struct tagAlloc *m;          struct tagAlloc *m;
         register int i, act, inact;          register int i, act, inact;
           int len = 0;
   
         if (!mp || !cb)          if (!mp || !cb)
                return;                return -1;
   
         for (i = act = inact = 0; i < MEM_BUCKETS; act = inact = 0, i++) {          for (i = act = inact = 0; i < MEM_BUCKETS; act = inact = 0, i++) {
                 TAILQ_FOREACH(m, &mp->pool_active[i], alloc_node)                  TAILQ_FOREACH(m, &mp->pool_active[i], alloc_node)
Line 701  mpool_statistics(mpool_t * __restrict mp, mpool_stat_c Line 638  mpool_statistics(mpool_t * __restrict mp, mpool_stat_c
                 TAILQ_FOREACH(m, &mp->pool_inactive[i], alloc_node)                  TAILQ_FOREACH(m, &mp->pool_inactive[i], alloc_node)
                         inact++;                          inact++;
   
                cb(1 << (i + MEM_MIN_BUCKET), act, inact);                len += cb(1 << (i + MEM_MIN_BUCKET), act, inact, data, datlen);
         }          }
   
           return len;
 }  }
   
 /* ----------------------------------------------------------- */  /* ----------------------------------------------------------- */
Line 770  mpool_xstrdup(const char *str) Line 709  mpool_xstrdup(const char *str)
 }  }
   
 /*  /*
 * mpool_xstatistics() - elwix memory pool statistics wrapper * mpool_xstatistics() - elwix default memory pool statistics wrapper
  *   *
  * @cb = Export statistics to callback   * @cb = Export statistics to callback
 * return: none * @data = data buffer
  * @datlen = data buffer length
  * return: >0 data in string buffer
  */   */
voidint
mpool_xstatistics(mpool_stat_cb cb)mpool_xstatistics(mpool_stat_cb cb, void *data, u_int datlen)
 {  {
        mpool_statistics(elwix_mpool, cb);        return mpool_statistics(elwix_mpool, cb, data, datlen);
 }  }
   
static voidstatic int
xdump_show(u_int size, u_int act, u_int inact)xdump_show(u_int size, u_int act, u_int inact, void *data, u_int datlen)
 {  {
         if (!act && !inact)          if (!act && !inact)
                return;       /* skip empty bucket */                return 0;       /* skip empty bucket */
   
         if (size < 1024)          if (size < 1024)
                 printf("\t\t* BUCKET %uB size, %u active, %u inactive\n",                   printf("\t\t* BUCKET %uB size, %u active, %u inactive\n", 
Line 796  xdump_show(u_int size, u_int act, u_int inact) Line 737  xdump_show(u_int size, u_int act, u_int inact)
         else          else
                 printf("\t\t* BUCKET %uMB size, %u active, %u inactive\n",                   printf("\t\t* BUCKET %uMB size, %u active, %u inactive\n", 
                                 size / 1048576, act, inact);                                  size / 1048576, act, inact);
   
           return 0;
 }  }
   
   static int
   xdump_show2(u_int size, u_int act, u_int inact, void *data, u_int datlen)
   {
           char szStr[STRSIZ], *str = data;
           int len = 0;
   
           if (!data || !datlen)
                   return 0;       /* skip missing data buffer */
           if (!act && !inact)
                   return 0;       /* skip empty bucket */
   
           if (size < 1024)
                   len = snprintf(szStr, sizeof szStr, "\t\t* BUCKET %uB size, %u active, %u inactive\n", 
                                   size, act, inact);
           else if (size < 1048576)
                   len = snprintf(szStr, sizeof szStr, "\t\t* BUCKET %uKB size, %u active, %u inactive\n", 
                                   size / 1024, act, inact);
           else
                   len = snprintf(szStr, sizeof szStr, "\t\t* BUCKET %uMB size, %u active, %u inactive\n", 
                                   size / 1048576, act, inact);
   
           strlcat(str, szStr, datlen);
           return len;
   }
   
 /*  /*
  * mpool_dump() - Dump elwix memory pool statistics   * mpool_dump() - Dump elwix memory pool statistics
  *   *
Line 820  mpool_dump(mpool_t * __restrict mp, const char *fmt, . Line 788  mpool_dump(mpool_t * __restrict mp, const char *fmt, .
                 printf("\n%s(%d)\n", __func__, __LINE__);                  printf("\n%s(%d)\n", __func__, __LINE__);
   
         printf("------------------------------------------------------------\n");          printf("------------------------------------------------------------\n");
        printf( " ELWIX memory pool ::\n"        printf( " ELWIX memory pool %p ::\n"
                 "\t- quotas Current/Real/Max = %lu/%lu/%lu\n"                  "\t- quotas Current/Real/Max = %lu/%lu/%lu\n"
                 "\t- calls Alloc/Free/Cache = %lu/%lu/%lu\n"                  "\t- calls Alloc/Free/Cache = %lu/%lu/%lu\n"
                 "\t- bytes Alloc/Free/Cache = %lu/%lu/%lu\n"                  "\t- bytes Alloc/Free/Cache = %lu/%lu/%lu\n"
                "\t- buckets :\n",                 "\t- buckets :\n", mp, 
                 p->pool_quota.curr, p->pool_quota.real, p->pool_quota.max,                   p->pool_quota.curr, p->pool_quota.real, p->pool_quota.max, 
                 p->pool_calls.alloc, p->pool_calls.free, p->pool_calls.cache,                   p->pool_calls.alloc, p->pool_calls.free, p->pool_calls.cache, 
                 p->pool_bytes.alloc, p->pool_bytes.free, p->pool_bytes.cache);                  p->pool_bytes.alloc, p->pool_bytes.free, p->pool_bytes.cache);
   
        mpool_statistics(p, xdump_show);        mpool_statistics(p, xdump_show, NULL, 0);
         printf("------------------------------------------------------------\n");          printf("------------------------------------------------------------\n");
 }  }
   
   /*
    * mpool_dump2() - Dump elwix memory pool statistics to string
    *
    * @mp = memory pool, if =NULL dump elwix default memory pool
    * @str = string buffer
    * @strlen = string buffer length
    * return: >0 data in string buffer
    */
   int
   mpool_dump2(mpool_t * __restrict mp, char *str, int strlen)
   {
           int len;
           mpool_t *p = mp ? mp : elwix_mpool;
   
           len = snprintf(str, strlen, 
                           "------------------------------------------------------------\n");
           len += snprintf(str + len, strlen - len, " ELWIX memory pool %p ::\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", mp, 
                   p->pool_quota.curr, p->pool_quota.real, p->pool_quota.max, 
                   p->pool_calls.alloc, p->pool_calls.free, p->pool_calls.cache, 
                   p->pool_bytes.alloc, p->pool_bytes.free, p->pool_bytes.cache);
           len += mpool_statistics(p, xdump_show2, str + len, strlen - len);
           len += snprintf(str + len, strlen - len, 
                           "------------------------------------------------------------\n");
   
           return len;
   }

Removed from v.1.5.2.1  
changed lines
  Added in v.1.8.56.1


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