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

version 1.5, 2015/06/25 17:53:50 version 1.6, 2015/07/01 21:48:39
Line 247  mpool_malloc(mpool_t * __restrict mp, u_int size, cons Line 247  mpool_malloc(mpool_t * __restrict mp, u_int size, cons
 void *  void *
 mpool_realloc(mpool_t * __restrict mp, void * __restrict data, u_int newsize, const char *memname)  mpool_realloc(mpool_t * __restrict mp, void * __restrict data, u_int newsize, const char *memname)
 {  {
         struct tagAlloc *m, *tmp;  
         int idx, oidx;          int idx, oidx;
         void *p;          void *p;
        u_int align, osize;        u_int osize;
   
         /* if !data execute mpool_malloc() */          /* if !data execute mpool_malloc() */
         if (!data)          if (!data)
Line 264  mpool_realloc(mpool_t * __restrict mp, void * __restri Line 263  mpool_realloc(mpool_t * __restrict mp, void * __restri
         if (MEM_BADADDR(data) || MEM_CORRUPT(data)) {          if (MEM_BADADDR(data) || MEM_CORRUPT(data)) {
                 elwix_SetErr(EFAULT, "Corrupted memory address");                  elwix_SetErr(EFAULT, "Corrupted memory address");
                 return NULL;                  return NULL;
         } else {  
                 osize = ((u_int*)data)[-2] * sizeof(u_int);  
                 oidx = BucketIndex(osize);  
         }          }
         /* prepare new size */          /* prepare new size */
         if (newsize > MEM_ALLOC_MAX) {          if (newsize > MEM_ALLOC_MAX) {
                 elwix_SetErr(ENOMEM, "Memory size is too large");                  elwix_SetErr(ENOMEM, "Memory size is too large");
                 return NULL;                  return NULL;
         } else {  
                 newsize = (newsize + 3) & ~3;   /* must align to 4 because needed room for sentinels */  
                 idx = BucketIndex(newsize);  
         }          }
   
         mpool_lock(mp);          mpool_lock(mp);
   
           osize = ((u_int*)data)[-2] * sizeof(u_int);
           oidx = BucketIndex(osize);
           newsize = (newsize + 3) & ~3;   /* must align to 4 because needed room for sentinels */
           idx = BucketIndex(newsize);
   
         /* quota */          /* quota */
         if (mp->pool_quota.max &&           if (mp->pool_quota.max && 
                         (mp->pool_quota.curr + ((u_long) newsize - osize)) > mp->pool_quota.max) {                          (mp->pool_quota.curr + ((u_long) newsize - osize)) > mp->pool_quota.max) {
Line 287  mpool_realloc(mpool_t * __restrict mp, void * __restri Line 285  mpool_realloc(mpool_t * __restrict mp, void * __restri
                 return NULL;                  return NULL;
         }          }
   
        /* find old memory block */        if (oidx != idx) {
        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);                  mpool_unlock(mp);
                elwix_SetErr(EFAULT, "Memory block not found");                p = mpool_malloc(mp, newsize, memname);
                return NULL;                if (!p)
        }                        return NULL;
   
        /* try to reallocate memory block to new bucket */                memcpy(p, data, osize);
        if (oidx != idx) {                mpool_free(mp, data, 0);
                align = 1 << (idx + MEM_MIN_BUCKET);        } else {
                p = realloc(m->alloc_mem, align + 12);                p = data;
                if (!p) { 
                        LOGERR; 
   
                        /* restore to old bucket pulled memory block for reallocation */                ((u_int*) p)[-2] = newsize / sizeof(u_int);
                        TAILQ_INSERT_HEAD(&mp->pool_active[oidx], m, alloc_node);                ((u_int*) p)[newsize / sizeof(u_int)] = MEM_MAGIC_STOP;
                        /* statistics */ 
                        mp->pool_calls.alloc++; 
                        mp->pool_bytes.alloc += osize; 
   
                        mpool_unlock(mp);                mp->pool_bytes.alloc += (u_long) newsize - osize;
                        return NULL;                mp->pool_quota.curr += (u_long) newsize - osize;
                } 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);                mpool_unlock(mp);
        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)        return p;
                strlcpy(m->alloc_name, memname, sizeof m->alloc_name); 
 
        mpool_unlock(mp); 
        return mem_data(m, void*); 
 }  }
   
 /*  /*
Line 773  xdump_show(u_int size, u_int act, u_int inact) Line 734  xdump_show(u_int size, u_int act, u_int inact)
 }  }
   
 /*  /*
 * mpool_xdump() - Dump elwix memory pool statistics * mpool_dump() - Dump elwix memory pool statistics
  *   *
 * @fmt = format string * @mp = memory pool, if =NULL dump elwix default memory pool
  * @fmt = prefix info format string
  * @... = argument(s)   * @... = argument(s)
  * return: none   * return: none
  */   */
 void  void
mpool_xdump(const char *fmt, ...)mpool_dump(mpool_t * __restrict mp, const char *fmt, ...)
 {  {
         va_list lst;          va_list lst;
           mpool_t *p = mp ? mp : elwix_mpool;
   
         if (fmt) {          if (fmt) {
                 va_start(lst, fmt);                  va_start(lst, fmt);
Line 797  mpool_xdump(const char *fmt, ...) Line 760  mpool_xdump(const char *fmt, ...)
                 "\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", 
                elwix_mpool->pool_quota.curr, elwix_mpool->pool_quota.real, elwix_mpool->pool_quota.max,                 p->pool_quota.curr, p->pool_quota.real, p->pool_quota.max, 
                elwix_mpool->pool_calls.alloc, elwix_mpool->pool_calls.free,                 p->pool_calls.alloc, p->pool_calls.free, p->pool_calls.cache, 
                elwix_mpool->pool_calls.cache, elwix_mpool->pool_bytes.alloc,                 p->pool_bytes.alloc, p->pool_bytes.free, p->pool_bytes.cache);
                elwix_mpool->pool_bytes.free, elwix_mpool->pool_bytes.cache); 
   
        mpool_statistics(elwix_mpool, xdump_show);        mpool_statistics(p, xdump_show);
         printf("------------------------------------------------------------\n");          printf("------------------------------------------------------------\n");
 }  }
   

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


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