blob: af4c0c4c2fe42857bf08ebac774d61c8ca6c50e8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#pragma once
#include <stdlib.h>
#include <iostream>
template<class T>
T* calloc_or_die(size_t nmemb)
{
if (nmemb == 0)
return NULL;
void* data = calloc(nmemb, sizeof(T));
if (data == NULL) {
std::cerr << "internal error: memory allocation failed; dying!" << std::endl;
throw std::exception();
}
return (T*)data;
}
template<class T> T& calloc_or_die()
{ return *calloc_or_die<T>(1); }
inline void free_it(void* ptr) { if (ptr != NULL) free(ptr); }
|