Clapkit
Loading...
Searching...
No Matches
ckMemory.h
Go to the documentation of this file.
1/*
2 *
3 * Clapkit
4 * ----------------------------------------------------------------------
5 * A wrapper for creating a 'generalized' app for Classic MacOS
6 * that (hopefully) can be ported easily to other platforms.
7 *
8 * CKMemory
9 * ----------------------------------------------------------------------
10 * Memory-related functionality
11 *
12 */
13
14#pragma once
15
16#include "ckUtils.h"
17#include "dlmalloc.h"
18#include <Memory.h>
19#include <cstddef>
20
21bool __CKSafeCopyString(char** dest, const char* src, const char* func = 0, int line = 0, const char* file = 0);
22void CKMemoryUsage(size_t* totalAllocd, size_t* totalFreed, int* numOfPtrs);
24
25#ifdef kCKDEBUGMEMORY
26
void* __CKMalloc(const char* func, int line, const char* file, size_t size);
28
void __CKFree(const char* func, int line, const char* file, void* ptr); void* __CKNew(const char* func, int line, const char* file, size_t size); void __CKDelete(void* ptr, const char* func, int line, const char* file);
32
33template <typename T>
34inline void __CKDestroy(T* ptr, const char* file, const char* func, int line) noexcept {
35 if (ptr) {
36 ptr->~T();
37 __CKFree(func, line, file, ptr);
38 }
39}
40
41template <typename T>
42inline void __CKDestroyArray(T* ptr, int count, const char* file, const char* func, int line) noexcept {
43 if (ptr) {
44 for (int i = 0; i < count; ++i)
45 ptr[i].~T();
46 __CKFree(func, line, file, ptr);
47 }
48}
49
54#define CKMalloc(sz) __CKMalloc(__func__, __LINE__, __FILENAME__, (sz))
55
60#define CKFree(ptr) __CKFree(__func__, __LINE__, __FILENAME__, (ptr))
61
66#define CKNew new (__func__, __LINE__, __FILENAME__)
67
72#define CKDelete(ptr) __CKDestroy(ptr, __FILENAME__, __func__, __LINE__)
73
74#define CKNewArray new (__func__, __LINE__, __FILENAME__)
75#define CKDeleteArray(ptr, count) \
76 __CKDestroyArray(ptr, (count), __FILENAME__, __func__, __LINE__)
77
78inline void* operator new(size_t size, const char* func, int line, const char* file) {
79 return __CKNew(func, line, file, size);
80}
81inline void* operator new[](size_t size, const char* func, int line, const char* file) {
82 return __CKNew(func, line, file, size);
83}
84
93#define CKSafeCopyString(dest, src) __CKSafeCopyString(&dest, src, __func__, __LINE__, __FILENAME__)
94
95#else
96
97#define CKMalloc(size) dlmalloc(size)
98#define CKFree(ptr) dlfree(ptr)
99#define CKNew new
100#define CKDelete(ptr) delete ptr
101#define CKNewArray new[]
102#define CKDeleteArray(ptr, count) delete[] ptr
103
104#define CKSafeCopyString(dest, src) __CKSafeCopyString(&dest, src, 0, 0, 0)
105
106#endif
void CKMemoryDumpLeaks()
Definition ckMemory.cpp:119
void __CKFree(const char *func, int line, const char *file, void *ptr)
Definition ckMemory.cpp:93
void * __CKNew(const char *func, int line, const char *file, size_t size)
Definition ckMemory.cpp:68
bool __CKSafeCopyString(char **dest, const char *src, const char *func=0, int line=0, const char *file=0)
Definition ckMemory.cpp:154
void __CKDelete(void *ptr, const char *func, int line, const char *file)
Definition ckMemory.cpp:76
void __CKDestroy(T *ptr, const char *file, const char *func, int line) noexcept
Definition ckMemory.h:34
void __CKDestroyArray(T *ptr, int count, const char *file, const char *func, int line) noexcept
Definition ckMemory.h:42
void * __CKMalloc(const char *func, int line, const char *file, size_t size)
Definition ckMemory.cpp:84
void CKMemoryUsage(size_t *totalAllocd, size_t *totalFreed, int *numOfPtrs)
Definition ckMemory.cpp:103