From: blackh <gra...@li...> - 2003-06-05 12:28:26
|
blackh Thu Jun 5 05:28:25 2003 EDT Modified files: /grapevine/cpp Allocator.h Log: This should fix the memory leak I just introduced, but I haven't tested it yet. Index: grapevine/cpp/Allocator.h diff -u grapevine/cpp/Allocator.h:1.1 grapevine/cpp/Allocator.h:1.2 --- grapevine/cpp/Allocator.h:1.1 Thu Jun 5 04:45:08 2003 +++ grapevine/cpp/Allocator.h Thu Jun 5 05:28:24 2003 @@ -1,4 +1,4 @@ -// $Id: Allocator.h,v 1.1 2003/06/05 11:45:08 blackh Exp $ +// $Id: Allocator.h,v 1.2 2003/06/05 12:28:24 blackh Exp $ #ifndef _ALLOCATOR_H_ #define _ALLOCATOR_H_ @@ -32,17 +32,17 @@ * since most of our blocks are tiny. */ -#define GRAPEVINE_BLOCKSIZES 32*sizeof(void*) /* The number of different block sizes */ +#define GRAPEVINE_BLOCKSIZES 32 /* The number of different block sizes */ #define GRAPEVINE_ATONCE 32 /* Allocate this many blocks at once */ extern void* grapevineFreeLists[GRAPEVINE_BLOCKSIZES]; inline void* grapevineAllocateSmall(size_t size) { - size = (size + (sizeof(void*)-1)) & ~(sizeof(void*)-1); - assert(size >= sizeof(void*)); - assert(size < GRAPEVINE_BLOCKSIZES); - void*& freeList = grapevineFreeLists[size]; + int idx = (size + (sizeof(void*)-1)) / sizeof(void*); + assert(idx >= 1); + assert(idx < GRAPEVINE_BLOCKSIZES); + void*& freeList = grapevineFreeLists[idx]; if (freeList == NULL) { char* buf = new char[size*GRAPEVINE_ATONCE]; void* p = NULL; @@ -60,10 +60,10 @@ inline void grapevineFreeSmall(void* p, size_t size) { - size = (size + (sizeof(void*)-1)) & ~(sizeof(void*)-1); - assert(size >= sizeof(void*)); - assert(size < GRAPEVINE_BLOCKSIZES); - void*& freeList = grapevineFreeLists[size]; + int idx = (size + (sizeof(void*)-1)) / sizeof(void*); + assert(idx >= 1); + assert(idx < GRAPEVINE_BLOCKSIZES); + void*& freeList = grapevineFreeLists[idx]; *((void**)p) = freeList; freeList = p; @@ -71,7 +71,7 @@ inline void* grapevineAllocateBig(size_t size) { - if (size < GRAPEVINE_BLOCKSIZES) + if (size <= (GRAPEVINE_BLOCKSIZES-1)*sizeof(void*)) return grapevineAllocateSmall(size); else return malloc(size); @@ -79,7 +79,7 @@ inline void grapevineFreeBig(void* p, size_t size) { - if (size < GRAPEVINE_BLOCKSIZES) + if (size <= (GRAPEVINE_BLOCKSIZES-1)*sizeof(void*)) return grapevineFreeSmall(p, size); else return free(p); @@ -106,9 +106,9 @@ void deallocate(void* p, size_type n) { - grapevineFreeBig(p, n); + grapevineFreeBig(p, n * sizeof(T)); } - + pointer address(reference x) const { return &x; } const_pointer address(const_reference x) const { return &x; } grapevine_allocator<T>& operator=(const grapevine_allocator&) { return *this; } |
From: blackh <gra...@li...> - 2003-06-05 21:18:56
|
blackh Thu Jun 5 14:18:50 2003 EDT Modified files: /grapevine/cpp Allocator.h Log: This will probably fix it. Index: grapevine/cpp/Allocator.h diff -u grapevine/cpp/Allocator.h:1.2 grapevine/cpp/Allocator.h:1.3 --- grapevine/cpp/Allocator.h:1.2 Thu Jun 5 05:28:24 2003 +++ grapevine/cpp/Allocator.h Thu Jun 5 14:18:49 2003 @@ -1,4 +1,4 @@ -// $Id: Allocator.h,v 1.2 2003/06/05 12:28:24 blackh Exp $ +// $Id: Allocator.h,v 1.3 2003/06/05 21:18:49 blackh Exp $ #ifndef _ALLOCATOR_H_ #define _ALLOCATOR_H_ @@ -44,6 +44,7 @@ assert(idx < GRAPEVINE_BLOCKSIZES); void*& freeList = grapevineFreeLists[idx]; if (freeList == NULL) { + size = idx*sizeof(void*); char* buf = new char[size*GRAPEVINE_ATONCE]; void* p = NULL; for (int i = 0; i < GRAPEVINE_ATONCE; i++) { |
From: blackh <gra...@li...> - 2003-06-13 23:58:23
|
blackh Fri Jun 13 16:58:22 2003 EDT Modified files: /grapevine/cpp Allocator.h Log: Index: grapevine/cpp/Allocator.h diff -u grapevine/cpp/Allocator.h:1.7 grapevine/cpp/Allocator.h:1.8 --- grapevine/cpp/Allocator.h:1.7 Fri Jun 13 11:56:13 2003 +++ grapevine/cpp/Allocator.h Fri Jun 13 16:58:22 2003 @@ -1,4 +1,4 @@ -// $Id: Allocator.h,v 1.7 2003/06/13 18:56:13 rossta Exp $ +// $Id: Allocator.h,v 1.8 2003/06/13 23:58:22 blackh Exp $ #ifndef _ALLOCATOR_H_ #define _ALLOCATOR_H_ @@ -104,7 +104,7 @@ pointer allocate(size_type n, const void * = 0) { - return (T*) n != 0 ? grapevineAllocateBig(n * sizeof(T)) : 0; + return n != 0 ? (T*) grapevineAllocateBig(n * sizeof(T)) : (T*) 0; } void deallocate(void* p, size_type n) |
From: rossta <gra...@li...> - 2004-01-15 07:35:56
|
rossta Wed Jan 14 23:35:56 2004 EDT Modified files: /grapevine/cpp Allocator.h Log: MSVC version runs now, but it leaks memory like crazy. Needs research Index: grapevine/cpp/Allocator.h diff -u grapevine/cpp/Allocator.h:1.13 grapevine/cpp/Allocator.h:1.14 --- grapevine/cpp/Allocator.h:1.13 Tue Jan 13 22:19:26 2004 +++ grapevine/cpp/Allocator.h Wed Jan 14 23:35:55 2004 @@ -1,4 +1,4 @@ -// $Id: Allocator.h,v 1.13 2004/01/14 06:19:26 rossta Exp $ +// $Id: Allocator.h,v 1.14 2004/01/15 07:35:55 rossta Exp $ #ifndef _ALLOCATOR_H_ #define _ALLOCATOR_H_ @@ -75,9 +75,9 @@ assert(idx < GRAPEVINE_BLOCKSIZES); void*& freeList = grapevineFreeLists[idx]; - if (p != NULL) { +// if (p != NULL) { *((void**)p) = freeList; - } +// } freeList = p; } @@ -113,7 +113,7 @@ free(p); } -#ifndef _MSC_VER +// #ifndef _MSC_VER template <class T> class grapevine_allocator @@ -136,7 +136,11 @@ void deallocate(void* p, size_type n) { - assert(p != 0); +#ifdef _MSC_VER + if (p == 0) + return; +#endif + assert(p != 0); grapevineFreeBig(p, n * sizeof(T)); } @@ -174,17 +178,28 @@ { typedef grapevine_allocator<U> other; }; + +#ifndef _MSC_VER template <class U> grapevine_allocator(const grapevine_allocator<U>&) {} - template <class U> grapevine_allocator& operator=(const grapevine_allocator<U>&) - { - return *this; - } + template <class U> grapevine_allocator& operator=(const grapevine_allocator<U>&) { return *this; } + +#else + typedef _PDFT difference_type; + + char _FARQ *_Charalloc(size_type _N) + { + return reinterpret_cast<char _FARQ *>(grapevineAllocateBig((difference_type)_N * sizeof(T))); + } + +#endif }; + +#if 0 -#else // _MSC_VER +// #else // _MSC_VER /* @@ -300,7 +315,9 @@ void deallocate(void _FARQ *_P, size_type _N) { -// assert(_P != 0); + if (_P == NULL) + return; + assert(_P != 0); grapevineFreeBig(_P, _N * sizeof(_Ty)); } |