| 1 |
|
| 2 |
#define _PROTO_CHECK_IMPL |
| 3 |
#include "protoCheck.h" |
| 4 |
|
| 5 |
#include "protoDispatcher.h" // for ProtoDispatcher::Mutex to provide thread safety |
| 6 |
|
| 7 |
#include <exception> |
| 8 |
#include <cstdlib> |
| 9 |
|
| 10 |
#include <stdio.h> // for fprintf() |
| 11 |
#include <string.h> // for strncpy() |
| 12 |
#include <map> // for std::map<> |
| 13 |
|
| 14 |
class ProtoCheckItem |
| 15 |
{ |
| 16 |
public: |
| 17 |
ProtoCheckItem(); |
| 18 |
ProtoCheckItem(size_t size, const char* file, int line); |
| 19 |
ProtoCheckItem(const ProtoCheckItem& item); |
| 20 |
~ProtoCheckItem(); |
| 21 |
|
| 22 |
ProtoCheckItem& operator=(const ProtoCheckItem& item); |
| 23 |
|
| 24 |
size_t GetSize() const |
| 25 |
{return mem_size;} |
| 26 |
const char* GetFile() const |
| 27 |
{return file_name;} |
| 28 |
int GetLine() const |
| 29 |
{return line_num;} |
| 30 |
|
| 31 |
void MarkLogged() |
| 32 |
{mark_logged = true;} |
| 33 |
void UnmarkLogged() |
| 34 |
{mark_logged = false;} |
| 35 |
bool WasLogged() const |
| 36 |
{return mark_logged;} |
| 37 |
void MarkDeleted() |
| 38 |
{mark_deleted = true;} |
| 39 |
bool WasDeleted() const |
| 40 |
{return mark_deleted;} |
| 41 |
|
| 42 |
void SetDeletionInfo(const char* file, int line) |
| 43 |
{ |
| 44 |
if (NULL != file) |
| 45 |
strncpy(deletion_file, file, 255); |
| 46 |
else |
| 47 |
deletion_file[0] = '\0'; |
| 48 |
deletion_line = line; |
| 49 |
} |
| 50 |
const char* GetDeletionFile() const |
| 51 |
{return deletion_file;} |
| 52 |
int GetDeletionLine() const |
| 53 |
{return deletion_line;} |
| 54 |
|
| 55 |
// These are provided to avoid new / delete within locks |
| 56 |
static ProtoCheckItem* Create(size_t size, const char* file, int line) |
| 57 |
{ |
| 58 |
ProtoCheckItem* item = (ProtoCheckItem*)calloc(1, sizeof(ProtoCheckItem)); |
| 59 |
item->mem_size = size; |
| 60 |
item->file_name[255] = '\0'; |
| 61 |
item->line_num = line; |
| 62 |
item->mark_logged = false; |
| 63 |
item->mark_deleted = false; |
| 64 |
item->deletion_file[0] = item->deletion_file[255] = '\0'; |
| 65 |
item->deletion_line = 0; |
| 66 |
return item; |
| 67 |
} |
| 68 |
static void Destroy(ProtoCheckItem* item) |
| 69 |
{free(item);} |
| 70 |
|
| 71 |
private: |
| 72 |
size_t mem_size; |
| 73 |
char file_name[256]; // TBD PATH_MAX |
| 74 |
int line_num; |
| 75 |
bool mark_logged; |
| 76 |
bool mark_deleted; |
| 77 |
char deletion_file[256]; |
| 78 |
int deletion_line; |
| 79 |
|
| 80 |
}; // end class ProtoCheckItem() |
| 81 |
|
| 82 |
typedef std::map<const void* , ProtoCheckItem> ProtoCheckMap; |
| 83 |
|
| 84 |
class ProtoChecker |
| 85 |
{ |
| 86 |
public: |
| 87 |
ProtoChecker(); |
| 88 |
~ProtoChecker(); |
| 89 |
|
| 90 |
bool GetMapLock() const |
| 91 |
{return map_is_locked;} |
| 92 |
|
| 93 |
void AddItem(void* ptr, size_t size, const char* file, int line) |
| 94 |
{ |
| 95 |
ProtoDispatcher::Lock(check_map_mutex); |
| 96 |
if (map_is_locked) |
| 97 |
{ |
| 98 |
// ignore reentrant new/delete from ourselves |
| 99 |
ProtoDispatcher::Unlock(check_map_mutex); |
| 100 |
return; |
| 101 |
} |
| 102 |
map_is_locked = true; |
| 103 |
//ProtoCheckItem* item = ProtoCheckItem::Create(size, file, line); |
| 104 |
check_map[ptr] = ProtoCheckItem(size, file, line); |
| 105 |
map_is_locked = false; |
| 106 |
ProtoDispatcher::Unlock(check_map_mutex); |
| 107 |
} |
| 108 |
|
| 109 |
void CacheInfo(const char* file, int line) |
| 110 |
{ |
| 111 |
// Used to cache info for upcoming delete call |
| 112 |
ProtoDispatcher::Lock(check_map_mutex); |
| 113 |
if (cache_lock) |
| 114 |
{ |
| 115 |
ProtoDispatcher::Unlock(check_map_mutex); |
| 116 |
return; |
| 117 |
} |
| 118 |
cache_lock = true; // will reset in corresponding DeleteItem() call |
| 119 |
if (NULL != cached_file) |
| 120 |
free(cached_file); |
| 121 |
if (NULL != (cached_file = (char*)malloc((strlen(file)+1)*sizeof(char)))) |
| 122 |
strcpy(cached_file, file); |
| 123 |
cached_line = line; |
| 124 |
//ProtoDispatcher::Unlock(check_map_mutex); |
| 125 |
} |
| 126 |
void ClearCacheInfo() |
| 127 |
{ |
| 128 |
cached_file[0] = '\0'; |
| 129 |
cached_line = 0; |
| 130 |
} |
| 131 |
const char* GetCachedFile() const |
| 132 |
{return cached_file;} |
| 133 |
int GetCachedLine() const |
| 134 |
{return cached_line;} |
| 135 |
|
| 136 |
void DeleteItem(void* ptr) |
| 137 |
{ |
| 138 |
ProtoDispatcher::Lock(check_map_mutex); |
| 139 |
if (map_is_locked) |
| 140 |
{ |
| 141 |
// ignore reentrant new/delete from ourselves |
| 142 |
ProtoDispatcher::Unlock(check_map_mutex); |
| 143 |
return; |
| 144 |
} |
| 145 |
map_is_locked = true; |
| 146 |
ProtoCheckMap::iterator it = check_map.find(ptr); |
| 147 |
const char* file = NULL; |
| 148 |
int line = 0; |
| 149 |
if (cache_lock) |
| 150 |
{ |
| 151 |
file = GetCachedFile(); |
| 152 |
line = GetCachedLine(); |
| 153 |
} |
| 154 |
char text[512]; |
| 155 |
if (it != check_map.end()) |
| 156 |
{ |
| 157 |
//ProtoCheckItem::Destroy(it->second); |
| 158 |
/*sprintf(text, "found item %p to delete %s %d (allocated at %s %d)", |
| 159 |
ptr, file, line, it->second.GetFile(), it->second.GetLine()); |
| 160 |
perror(text); |
| 161 |
it->second.SetDeletionInfo(GetCachedFile(), GetCachedLine()); |
| 162 |
it->second.MarkDeleted();*/ |
| 163 |
check_map.erase(ptr); |
| 164 |
} |
| 165 |
else |
| 166 |
{ |
| 167 |
sprintf(text, "couldn't find item %p to delete %s %d", ptr, file, line); |
| 168 |
perror(text); |
| 169 |
} |
| 170 |
if (cache_lock) |
| 171 |
{ |
| 172 |
//ClearCacheInfo(); |
| 173 |
cache_lock = false; |
| 174 |
ProtoDispatcher::Unlock(check_map_mutex); |
| 175 |
} |
| 176 |
//check_map.erase(ptr); |
| 177 |
map_is_locked = false; |
| 178 |
ProtoDispatcher::Unlock(check_map_mutex); |
| 179 |
} |
| 180 |
|
| 181 |
void Reset() |
| 182 |
{ |
| 183 |
ProtoDispatcher::Lock(check_map_mutex); |
| 184 |
if (map_is_locked) |
| 185 |
{ |
| 186 |
// ignore reentrant new/delete from ourselves |
| 187 |
ProtoDispatcher::Unlock(check_map_mutex); |
| 188 |
return; |
| 189 |
} |
| 190 |
map_is_locked = true; |
| 191 |
check_map.clear(); |
| 192 |
map_is_locked = false; |
| 193 |
ProtoDispatcher::Unlock(check_map_mutex); |
| 194 |
} |
| 195 |
void LogAllocations(FILE* filePtr); |
| 196 |
|
| 197 |
private: |
| 198 |
ProtoCheckMap check_map; |
| 199 |
ProtoDispatcher::Mutex check_map_mutex; |
| 200 |
bool map_is_locked; |
| 201 |
bool cache_lock; |
| 202 |
char* cached_file; |
| 203 |
int cached_line; |
| 204 |
}; |
| 205 |
|
| 206 |
ProtoChecker::ProtoChecker() |
| 207 |
: map_is_locked(false), cache_lock(false), cached_file(NULL), cached_line(0) |
| 208 |
{ |
| 209 |
#ifdef UNIX |
| 210 |
pthread_mutexattr_t attr; |
| 211 |
pthread_mutexattr_init(&attr); |
| 212 |
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); |
| 213 |
pthread_mutex_init(&check_map_mutex, &attr); |
| 214 |
#else |
| 215 |
ProtoDispatcher::Init(check_map_mutex); // Windows critical sections are recursive |
| 216 |
#endif // UNIX / WIN32 |
| 217 |
} |
| 218 |
|
| 219 |
ProtoChecker::~ProtoChecker() |
| 220 |
{ |
| 221 |
TRACE("enter ProtoChecker destructor ...\n"); |
| 222 |
check_map.clear(); |
| 223 |
ProtoDispatcher::Destroy(check_map_mutex); |
| 224 |
TRACE("exit ProtoChecker destructor.n"); |
| 225 |
} |
| 226 |
|
| 227 |
void ProtoChecker::LogAllocations(FILE* filePtr) |
| 228 |
{ |
| 229 |
ProtoDispatcher::Lock(check_map_mutex); |
| 230 |
for (ProtoCheckMap::iterator it=check_map.begin(); it!=check_map.end(); ++it) |
| 231 |
{ |
| 232 |
const void* ptr = it->first; |
| 233 |
ProtoCheckItem& item = it->second; |
| 234 |
if (!item.WasLogged() || item.WasDeleted()) |
| 235 |
{ |
| 236 |
const char* action; |
| 237 |
const char* file; |
| 238 |
int line; |
| 239 |
if (item.WasDeleted()) |
| 240 |
{ |
| 241 |
action = "deleted"; |
| 242 |
file = item.GetDeletionFile(); |
| 243 |
line = item.GetDeletionLine(); |
| 244 |
} |
| 245 |
else |
| 246 |
{ |
| 247 |
action = "allocated"; |
| 248 |
file = item.GetFile(); |
| 249 |
line = item.GetLine(); |
| 250 |
} |
| 251 |
int result = fprintf(filePtr, "ProtoCheck: %s %lu bytes for ptr %p from %s:%d\n", |
| 252 |
action, (unsigned long)item.GetSize(), ptr, file, line); |
| 253 |
if (result < 0) |
| 254 |
{ |
| 255 |
char buffer[1024]; |
| 256 |
sprintf(buffer, "ProtoCheck: %s %lu bytes for ptr %p from %s:%d", |
| 257 |
action, (unsigned long)item.GetSize(), ptr, file, line); |
| 258 |
perror(buffer); |
| 259 |
} |
| 260 |
//item.MarkLogged(); |
| 261 |
if (item.WasDeleted()) |
| 262 |
{ |
| 263 |
check_map.erase(ptr); |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
ProtoDispatcher::Unlock(check_map_mutex); |
| 268 |
} // end ProtoChecker::LogAllocations() |
| 269 |
|
| 270 |
|
| 271 |
ProtoCheckItem::ProtoCheckItem() |
| 272 |
: mem_size(0), line_num(0), mark_logged(false), |
| 273 |
mark_deleted(false), deletion_line(0) |
| 274 |
{ |
| 275 |
file_name[0] = file_name[255] = '\0'; |
| 276 |
deletion_file[0] = deletion_file[255] = '\0'; |
| 277 |
} |
| 278 |
|
| 279 |
ProtoCheckItem::ProtoCheckItem(size_t size, const char* file, int line) |
| 280 |
: mem_size(size), line_num(line), mark_logged(false), |
| 281 |
mark_deleted(false), deletion_line(0) |
| 282 |
{ |
| 283 |
file_name[255] = '\0'; |
| 284 |
strncpy(file_name, file, 255); |
| 285 |
deletion_file[0] = deletion_file[255] = '\0'; |
| 286 |
} |
| 287 |
|
| 288 |
ProtoCheckItem::ProtoCheckItem(const ProtoCheckItem& item) |
| 289 |
: mem_size(item.mem_size), line_num(item.line_num), mark_logged(item.mark_logged), |
| 290 |
mark_deleted(item.mark_deleted), deletion_line(item.deletion_line) |
| 291 |
{ |
| 292 |
file_name[255] = '\0'; |
| 293 |
strncpy(file_name, item.file_name, 255); |
| 294 |
deletion_file[255] = '\0'; |
| 295 |
strncpy(deletion_file, item.deletion_file, 255); |
| 296 |
|
| 297 |
} |
| 298 |
|
| 299 |
ProtoCheckItem& ProtoCheckItem::operator=(const ProtoCheckItem& item) |
| 300 |
{ |
| 301 |
mem_size = item.mem_size; |
| 302 |
strcpy(file_name, item.file_name); |
| 303 |
line_num = item.line_num; |
| 304 |
mark_logged = item.mark_logged; |
| 305 |
mark_deleted = item.mark_deleted; |
| 306 |
strcpy(deletion_file, item.deletion_file); |
| 307 |
return *this; |
| 308 |
} |
| 309 |
|
| 310 |
ProtoCheckItem::~ProtoCheckItem() |
| 311 |
{ |
| 312 |
} |
| 313 |
|
| 314 |
#ifdef USE_PROTO_CHECK |
| 315 |
|
| 316 |
static ProtoChecker proto_checker; |
| 317 |
|
| 318 |
void ProtoCheckLogAllocations(FILE* filePtr) |
| 319 |
{ |
| 320 |
//if (NULL == proto_checker) proto_checker = new ProtoChecker; |
| 321 |
proto_checker.LogAllocations(filePtr); |
| 322 |
} // end ProtoCheckLogAllocations() |
| 323 |
|
| 324 |
void ProtoCheckResetLogging() |
| 325 |
{ |
| 326 |
TRACE("enter ProtoCheckResetLogging() ...\n"); |
| 327 |
//if (NULL == proto_checker) proto_checker = new ProtoChecker; |
| 328 |
proto_checker.Reset(); |
| 329 |
TRACE("exit ProtoCheckResetLogging()\n"); |
| 330 |
} // end ProtoCheckResetLogging() |
| 331 |
|
| 332 |
void ProtoCheckCacheInfo(const char* file, int line) |
| 333 |
{ |
| 334 |
//if (NULL == proto_checker) proto_checker = new ProtoChecker; |
| 335 |
//TRACE("caching deletion info %s %d ...\n", file, line); |
| 336 |
proto_checker.CacheInfo(file, line); |
| 337 |
} // end ProtoCheckCacheInfo() |
| 338 |
|
| 339 |
void* operator new(size_t size, const char* file, int line) |
| 340 |
{ |
| 341 |
void* p = malloc(size); |
| 342 |
if (0 == p) |
| 343 |
throw std::bad_alloc(); |
| 344 |
//if (NULL == proto_checker) proto_checker = new ProtoChecker; |
| 345 |
proto_checker.AddItem(p, size, file, line); |
| 346 |
return p; |
| 347 |
} |
| 348 |
|
| 349 |
void* operator new[](size_t size, const char* file, int line) |
| 350 |
{ |
| 351 |
void* p = malloc(size); |
| 352 |
if (0 == p) |
| 353 |
throw std::bad_alloc(); |
| 354 |
//if (NULL == proto_checker) proto_checker = new ProtoChecker; |
| 355 |
proto_checker.AddItem(p, size, file, line); |
| 356 |
return p; |
| 357 |
} |
| 358 |
|
| 359 |
void operator delete(void* p) throw() |
| 360 |
{ |
| 361 |
//if (NULL == proto_checker) proto_checker = new ProtoChecker; |
| 362 |
//TRACE("operator delete (map_lock:%d)\n", proto_checker.GetMapLock()); |
| 363 |
proto_checker.DeleteItem(p); |
| 364 |
free(p); |
| 365 |
} |
| 366 |
|
| 367 |
void operator delete[](void* p) throw() |
| 368 |
{ |
| 369 |
//if (NULL == proto_checker) proto_checker = new ProtoChecker; |
| 370 |
//TRACE("operator delete[] (map_lock:%d)\n", proto_checker.GetMapLock()); |
| 371 |
proto_checker.DeleteItem(p); |
| 372 |
free(p); |
| 373 |
} |
| 374 |
|
| 375 |
#endif // USE_PROTO_CHECK |