| 1 |
#ifndef __CRITCL_UTIL_ALLOC_H |
| 2 |
#define __CRITCL_UTIL_ALLOC_H 1 |
| 3 |
|
| 4 |
/* |
| 5 |
* Copyright (c) 2017-2020 Andreas Kupries <[email protected]> |
| 6 |
* = = == === ===== ======== ============= ===================== |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <string.h> /* memcpy - See STREP */ |
| 10 |
#include <tcl.h> |
| 11 |
|
| 12 |
/* |
| 13 |
* Helper macros for easy allocation of structures and arrays. |
| 14 |
*/ |
| 15 |
|
| 16 |
#define ALLOC(type) (type *) ckalloc (sizeof (type)) |
| 17 |
#define ALLOC_PLUS(type,n) (type *) ckalloc (sizeof (type) + (n)) |
| 18 |
#define NALLOC(type,n) (type *) ckalloc (sizeof (type) * (n)) |
| 19 |
#define REALLOC(x,type,n) (type *) ckrealloc ((char*) x, sizeof (type) * (n)) |
| 20 |
|
| 21 |
#define FREE(p) ckfree ((char*)(p)) |
| 22 |
|
| 23 |
/* |
| 24 |
* Macros to properly set a string rep from a string or DString. The main |
| 25 |
* point is adding the terminating \0 character. The Tcl core checks for that. |
| 26 |
*/ |
| 27 |
|
| 28 |
#define STREP(o,str,len) \ |
| 29 |
(o)->length = (len); \ |
| 30 |
(o)->bytes = ckalloc((len)+1); \ |
| 31 |
memcpy ((o)->bytes, (str), (len)); \ |
| 32 |
(o)->bytes[(len)] = '\0' |
| 33 |
|
| 34 |
#define STREP_DS(o,ds) { \ |
| 35 |
int length = Tcl_DStringLength (ds); \ |
| 36 |
STREP(o, Tcl_DStringValue (ds), length); \ |
| 37 |
} |
| 38 |
|
| 39 |
#define STRDUP(v,s) { \ |
| 40 |
char* str = ckalloc (1+strlen (s)); \ |
| 41 |
strcpy (str, s); \ |
| 42 |
v = str; \ |
| 43 |
} |
| 44 |
|
| 45 |
/* |
| 46 |
* = = == === ===== ======== ============= ===================== |
| 47 |
*/ |
| 48 |
|
| 49 |
#endif /* __CRITCL_UTIL_ALLOC_H */ |
| 50 |
|
| 51 |
/* |
| 52 |
* Local Variables: |
| 53 |
* mode: c |
| 54 |
* c-basic-offset: 4 |
| 55 |
* fill-column: 78 |
| 56 |
* End: |
| 57 |
*/ |