forked from aburch/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimmem.h
28 lines (22 loc) · 943 Bytes
/
simmem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
* Copyright (c) 1997 - 2001 Hansjörg Malthaner
*
* This file is part of the Simutrans project under the artistic license.
* (see license.txt)
*/
#ifndef SIMMEM_H
#define SIMMEM_H
#include <stddef.h>
#ifdef _MSC_VER
# include <malloc.h>
# define guarded_free free
#else
void guarded_free(void* ptr);
#endif
void* xmalloc(size_t size); // Throws std::bad_alloc on failure
void* xrealloc(void * const ptr, size_t size); // Throws std::bad_alloc on failure
#define MALLOC(type) ((type*)xmalloc(sizeof(type))) // Allocate an object of a certain type
#define MALLOCN(type, n) ((type*)xmalloc(sizeof(type) * (n))) // Allocate n objects of a certain type
#define MALLOCF(type, member, n) ((type*)xmalloc(offsetof(type, member) + sizeof(*((type*)0)->member) * (n)))
#define REALLOC(ptr, type, n) (type*)xrealloc((void * const)ptr, sizeof(type) * (n)) // Reallocate n objects of a certain type
#endif