cmpsc330hw4/raii.h

27 lines
415 B
C
Raw Normal View History

2024-11-20 00:46:02 +00:00
#ifndef RAII_H
#define RAII_H
#include "defs.h"
unsigned long allocatedMem = 0;
void *alloc(size_t size) {
void *ptr = malloc(size);
if (!ptr) {
pter "Memory allocation failed" << nl;
return NULL;
}
allocatedMem += 1;
return ptr;
}
void dealloc(void *ptr) {
if (ptr) free(ptr);
allocatedMem -= 1;
}
bool assert_alloc() {
return !allocatedMem;
}
#endif // RAII_H