Cleanup: trailing space guardedalloc & memutil

This commit is contained in:
Campbell Barton 2019-01-24 16:20:16 +11:00
parent d8a082f914
commit 4d29312c66
9 changed files with 46 additions and 51 deletions

View File

@ -48,11 +48,11 @@
* *
* There are currently no known issues with MEM. Note that there is a * There are currently no known issues with MEM. Note that there is a
* second intern/ module with MEM_ prefix, for use in c++. * second intern/ module with MEM_ prefix, for use in c++.
* *
* \subsection memdependencies Dependencies * \subsection memdependencies Dependencies
* - stdlib * - stdlib
* - stdio * - stdio
* *
* \subsection memdocs API Documentation * \subsection memdocs API Documentation
* See \ref MEM_guardedalloc.h * See \ref MEM_guardedalloc.h
*/ */
@ -146,11 +146,11 @@ extern "C" {
extern void *(*MEM_mapallocN)(size_t len, const char *str) /* ATTR_MALLOC */ ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2); extern void *(*MEM_mapallocN)(size_t len, const char *str) /* ATTR_MALLOC */ ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2);
/** Print a list of the names and sizes of all allocated memory /** Print a list of the names and sizes of all allocated memory
* blocks. as a python dict for easy investigation */ * blocks. as a python dict for easy investigation */
extern void (*MEM_printmemlist_pydict)(void); extern void (*MEM_printmemlist_pydict)(void);
/** Print a list of the names and sizes of all allocated memory /** Print a list of the names and sizes of all allocated memory
* blocks. */ * blocks. */
extern void (*MEM_printmemlist)(void); extern void (*MEM_printmemlist)(void);
/** calls the function on all allocated memory blocks. */ /** calls the function on all allocated memory blocks. */
@ -158,7 +158,7 @@ extern "C" {
/** Print statistics about memory usage */ /** Print statistics about memory usage */
extern void (*MEM_printmemlist_stats)(void); extern void (*MEM_printmemlist_stats)(void);
/** Set the callback function for error output. */ /** Set the callback function for error output. */
extern void (*MEM_set_error_callback)(void (*func)(const char *)); extern void (*MEM_set_error_callback)(void (*func)(const char *));
@ -171,7 +171,7 @@ extern "C" {
/** Set thread locking functions for safe memory allocation from multiple /** Set thread locking functions for safe memory allocation from multiple
* threads, pass NULL pointers to disable thread locking again. */ * threads, pass NULL pointers to disable thread locking again. */
extern void (*MEM_set_lock_callback)(void (*lock)(void), void (*unlock)(void)); extern void (*MEM_set_lock_callback)(void (*lock)(void), void (*unlock)(void));
/** Attempt to enforce OSX (or other OS's) to have malloc and stack nonzero */ /** Attempt to enforce OSX (or other OS's) to have malloc and stack nonzero */
extern void (*MEM_set_memory_debug)(void); extern void (*MEM_set_memory_debug)(void);

View File

@ -197,7 +197,7 @@ static const char *check_memlist(MemHead *memh);
/* --------------------------------------------------------------------- */ /* --------------------------------------------------------------------- */
/* vars */ /* vars */
/* --------------------------------------------------------------------- */ /* --------------------------------------------------------------------- */
static unsigned int totblock = 0; static unsigned int totblock = 0;
static size_t mem_in_use = 0, mmap_in_use = 0, peak_mem = 0; static size_t mem_in_use = 0, mmap_in_use = 0, peak_mem = 0;
@ -289,7 +289,7 @@ bool MEM_guarded_consistency_check(void)
/* check_memlist starts from the front, and runs until it finds /* check_memlist starts from the front, and runs until it finds
* the requested chunk. For this test, that's the last one. */ * the requested chunk. For this test, that's the last one. */
listend = membase->last; listend = membase->last;
err_val = check_memlist(listend); err_val = check_memlist(listend);
return (err_val == NULL); return (err_val == NULL);
@ -316,7 +316,7 @@ size_t MEM_guarded_allocN_len(const void *vmemh)
{ {
if (vmemh) { if (vmemh) {
const MemHead *memh = vmemh; const MemHead *memh = vmemh;
memh--; memh--;
return memh->len; return memh->len;
} }
@ -328,7 +328,7 @@ size_t MEM_guarded_allocN_len(const void *vmemh)
void *MEM_guarded_dupallocN(const void *vmemh) void *MEM_guarded_dupallocN(const void *vmemh)
{ {
void *newp = NULL; void *newp = NULL;
if (vmemh) { if (vmemh) {
const MemHead *memh = vmemh; const MemHead *memh = vmemh;
memh--; memh--;
@ -378,7 +378,7 @@ void *MEM_guarded_dupallocN(const void *vmemh)
void *MEM_guarded_reallocN_id(void *vmemh, size_t len, const char *str) void *MEM_guarded_reallocN_id(void *vmemh, size_t len, const char *str)
{ {
void *newp = NULL; void *newp = NULL;
if (vmemh) { if (vmemh) {
MemHead *memh = vmemh; MemHead *memh = vmemh;
memh--; memh--;
@ -485,7 +485,7 @@ static void print_memhead_backtrace(MemHead *memh)
static void make_memhead_header(MemHead *memh, size_t len, const char *str) static void make_memhead_header(MemHead *memh, size_t len, const char *str)
{ {
MemTail *memt; MemTail *memt;
memh->tag1 = MEMTAG1; memh->tag1 = MEMTAG1;
memh->name = str; memh->name = str;
memh->nextname = NULL; memh->nextname = NULL;
@ -522,7 +522,7 @@ void *MEM_guarded_mallocN(size_t len, const char *str)
MemHead *memh; MemHead *memh;
len = SIZET_ALIGN_4(len); len = SIZET_ALIGN_4(len);
memh = (MemHead *)malloc(len + sizeof(MemHead) + sizeof(MemTail)); memh = (MemHead *)malloc(len + sizeof(MemHead) + sizeof(MemTail));
if (LIKELY(memh)) { if (LIKELY(memh)) {
@ -785,7 +785,7 @@ void MEM_guarded_printmemlist_stats(void)
(double)pb->len / 1024.0 / (double)pb->items, pb->name); (double)pb->len / 1024.0 / (double)pb->items, pb->name);
} }
free(printblock); free(printblock);
mem_unlock_thread(); mem_unlock_thread();
#ifdef HAVE_MALLOC_STATS #ifdef HAVE_MALLOC_STATS
@ -823,7 +823,7 @@ static void MEM_guarded_printmemlist_internal(int pydict)
membl = membase->first; membl = membase->first;
if (membl) membl = MEMNEXT(membl); if (membl) membl = MEMNEXT(membl);
if (pydict) { if (pydict) {
print_error("# membase_debug.py\n"); print_error("# membase_debug.py\n");
print_error("membase = [\n"); print_error("membase = [\n");
@ -856,7 +856,7 @@ static void MEM_guarded_printmemlist_internal(int pydict)
print_error("]\n\n"); print_error("]\n\n");
print_error(mem_printmemlist_pydict_script); print_error(mem_printmemlist_pydict_script);
} }
mem_unlock_thread(); mem_unlock_thread();
} }
@ -940,7 +940,7 @@ void MEM_guarded_freeN(void *vmemh)
return; return;
} }
} }
memh--; memh--;
if (memh->tag1 == MEMFREE && memh->tag2 == MEMFREE) { if (memh->tag1 == MEMFREE && memh->tag2 == MEMFREE) {
MemorY_ErroR(memh->name, "double free"); MemorY_ErroR(memh->name, "double free");
@ -953,7 +953,7 @@ void MEM_guarded_freeN(void *vmemh)
{ {
memt = (MemTail *)(((char *) memh) + sizeof(MemHead) + memh->len); memt = (MemTail *)(((char *) memh) + sizeof(MemHead) + memh->len);
if (memt->tag3 == MEMTAG3) { if (memt->tag3 == MEMTAG3) {
memh->tag1 = MEMFREE; memh->tag1 = MEMFREE;
memh->tag2 = MEMFREE; memh->tag2 = MEMFREE;
memt->tag3 = MEMFREE; memt->tag3 = MEMFREE;

View File

@ -53,4 +53,3 @@ MEM_INLINE bool MEM_size_safe_multiply(size_t a, size_t b, size_t *result)
} }
#endif /* __MALLOCN_INLINE_H__ */ #endif /* __MALLOCN_INLINE_H__ */

View File

@ -4,7 +4,7 @@
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2 * as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version. * of the License, or (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
@ -214,7 +214,7 @@ static void *mmap_findlink(volatile mmapListBase *listbase, void *ptr)
if (ptr == NULL) return NULL; if (ptr == NULL) return NULL;
if (listbase == NULL) return NULL; if (listbase == NULL) return NULL;
mm = (MemMap *)listbase->first; mm = (MemMap *)listbase->first;
while (mm) { while (mm) {
if (mm->mmap == ptr) { if (mm->mmap == ptr) {

View File

@ -4,7 +4,7 @@
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2 * as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version. * of the License, or (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
@ -29,7 +29,7 @@
* \ingroup MEM * \ingroup MEM
* \author Andrea Weikert * \author Andrea Weikert
*/ */
#ifndef __MMAP_WIN_H__ #ifndef __MMAP_WIN_H__
#define __MMAP_WIN_H__ #define __MMAP_WIN_H__
@ -57,4 +57,3 @@ void *mmap(void *start, size_t len, int prot, int flags, int fd, off_t offset);
intptr_t munmap(void *ptr, size_t size); intptr_t munmap(void *ptr, size_t size);
#endif #endif

View File

@ -60,10 +60,10 @@ int main(int argc, char *argv[])
/* ----------------------------------------------------------------- */ /* ----------------------------------------------------------------- */
switch (argc) { switch (argc) {
case 2: case 2:
verbose = atoi(argv[1]); verbose = atoi(argv[1]);
if (verbose < 0) verbose = 0; if (verbose < 0) verbose = 0;
break; break;
case 1: case 1:
default: default:
verbose = 0; verbose = 0;
@ -77,7 +77,7 @@ int main(int argc, char *argv[])
/* ----------------------------------------------------------------- */ /* ----------------------------------------------------------------- */
/* flush mem lib output to stderr */ /* flush mem lib output to stderr */
MEM_set_error_callback(mem_error_cb); MEM_set_error_callback(mem_error_cb);
for (i = 0; i < NUM_BLOCKS; i++) { for (i = 0; i < NUM_BLOCKS; i++) {
int blocksize = 10000; int blocksize = 10000;
char tagstring[1000]; char tagstring[1000];
@ -99,7 +99,7 @@ int main(int argc, char *argv[])
else { else {
fprintf(stderr, "|--* Memory tested as good (as it should be)\n|\n"); fprintf(stderr, "|--* Memory tested as good (as it should be)\n|\n");
} }
} }
for (i = 0; i < NUM_BLOCKS; i++) { for (i = 0; i < NUM_BLOCKS; i++) {
MEM_freeN(p[i]); MEM_freeN(p[i]);
@ -124,11 +124,11 @@ int main(int argc, char *argv[])
for (i = 0; i< 1000; i++,ip++) *ip = i+1; for (i = 0; i< 1000; i++,ip++) *ip = i+1;
ip = (int*) p[6]; ip = (int*) p[6];
*(ip+10005) = 0; *(ip+10005) = 0;
retval = MEM_consistency_check(); retval = MEM_consistency_check();
/* the test should have failed */ /* the test should have failed */
error_status |= !retval; error_status |= !retval;
if (verbose) { if (verbose) {
if (retval) { if (retval) {
fprintf(stderr, "|--* Memory test failed (as it should be)\n"); fprintf(stderr, "|--* Memory test failed (as it should be)\n");
@ -136,8 +136,8 @@ int main(int argc, char *argv[])
else { else {
fprintf(stderr, "|--* Memory test FAILED to find corrupted blocks \n"); fprintf(stderr, "|--* Memory test FAILED to find corrupted blocks \n");
} }
} }
for (i = 0; i < NUM_BLOCKS; i++) { for (i = 0; i < NUM_BLOCKS; i++) {
MEM_freeN(p[i]); MEM_freeN(p[i]);
} }
@ -146,7 +146,7 @@ int main(int argc, char *argv[])
if (verbose && error_status) { if (verbose && error_status) {
fprintf(stderr,"|--* Memory was corrupted\n"); fprintf(stderr,"|--* Memory was corrupted\n");
} }
/* ----------------------------------------------------------------- */ /* ----------------------------------------------------------------- */
if (verbose) { if (verbose) {
if (error_status) { if (error_status) {
fprintf(stderr,"|\n|--* Errors were detected\n"); fprintf(stderr,"|\n|--* Errors were detected\n");
@ -154,10 +154,8 @@ int main(int argc, char *argv[])
else { else {
fprintf(stderr,"|\n|--* Test exited succesfully\n"); fprintf(stderr,"|\n|--* Test exited succesfully\n");
} }
fprintf(stderr,"|\n*** Finished test\n\n"); fprintf(stderr,"|\n*** Finished test\n\n");
} }
return error_status; return error_status;
} }

View File

@ -43,8 +43,8 @@ struct MEM_Allocator
typedef _Tp value_type; typedef _Tp value_type;
template<typename _Tp1> template<typename _Tp1>
struct rebind { struct rebind {
typedef MEM_Allocator<_Tp1> other; typedef MEM_Allocator<_Tp1> other;
}; };
MEM_Allocator() throw() {} MEM_Allocator() throw() {}
@ -75,16 +75,16 @@ struct MEM_Allocator
MEM_freeN(__p); MEM_freeN(__p);
} }
size_type max_size() const throw() { size_type max_size() const throw() {
return size_t(-1) / sizeof(_Tp); return size_t(-1) / sizeof(_Tp);
} }
void construct(pointer __p, const _Tp& __val) { void construct(pointer __p, const _Tp& __val) {
new(__p) _Tp(__val); new(__p) _Tp(__val);
} }
void destroy(pointer __p) { void destroy(pointer __p) {
__p->~_Tp(); __p->~_Tp();
} }
}; };

View File

@ -58,19 +58,19 @@ public:
{ {
} }
/** /**
* Returns the reference count of this object. * Returns the reference count of this object.
* @return the reference count. * @return the reference count.
*/ */
inline virtual int getRef() const; inline virtual int getRef() const;
/** /**
* Increases the reference count of this object. * Increases the reference count of this object.
* @return the new reference count. * @return the new reference count.
*/ */
inline virtual int incRef(); inline virtual int incRef();
/** /**
* Decreases the reference count of this object. * Decreases the reference count of this object.
* If the reference count reaches zero, the object self-destructs. * If the reference count reaches zero, the object self-destructs.
* @return the new reference count. * @return the new reference count.
@ -113,4 +113,3 @@ inline int MEM_RefCounted::decRef()
} }
#endif // __MEM_REFCOUNTED_H__ #endif // __MEM_REFCOUNTED_H__

View File

@ -48,21 +48,21 @@ extern "C" {
#endif #endif
/** /**
* Returns the reference count of this object. * Returns the reference count of this object.
* @param shared The object to query. * @param shared The object to query.
* @return The current reference count. * @return The current reference count.
*/ */
extern int MEM_RefCountedGetRef(MEM_TRefCountedObjectPtr shared); extern int MEM_RefCountedGetRef(MEM_TRefCountedObjectPtr shared);
/** /**
* Increases the reference count of this object. * Increases the reference count of this object.
* @param shared The object to query. * @param shared The object to query.
* @return The new reference count. * @return The new reference count.
*/ */
extern int MEM_RefCountedIncRef(MEM_TRefCountedObjectPtr shared); extern int MEM_RefCountedIncRef(MEM_TRefCountedObjectPtr shared);
/** /**
* Decreases the reference count of this object. * Decreases the reference count of this object.
* If the reference count reaches zero, the object self-destructs. * If the reference count reaches zero, the object self-destructs.
* @param shared The object to query. * @param shared The object to query.