1 /** 2 * Compiler implementation of the 3 * $(LINK2 https://www.dlang.org, D programming language). 4 * 5 * Copyright: Copyright (C) 1985-1998 by Symantec 6 * Copyright (C) 2000-2023 by The D Language Foundation, All Rights Reserved 7 * Authors: $(LINK2 https://www.digitalmars.com, Walter Bright) 8 * License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) 9 * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/backend/mem.d, backend/mem.d) 10 * Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/backend/mem.d 11 */ 12 13 14 module dmd.backend.mem; 15 16 import core.stdc.stdlib : malloc, calloc, realloc, free; 17 import core.stdc.string : strdup; 18 19 import dmd.backend.global : err_nomem; 20 21 extern (C): 22 23 nothrow: 24 @nogc: 25 @safe: 26 27 @trusted 28 char* mem_strdup(const char* s) 29 { 30 auto p = strdup(s); 31 if (!p && s) 32 err_nomem(); 33 return p; 34 } 35 36 @trusted 37 void* mem_malloc(size_t u) 38 { 39 auto p = malloc(u); 40 if (!p && u) 41 err_nomem(); 42 return p; 43 } 44 45 alias mem_fmalloc = mem_malloc; 46 47 @trusted 48 void* mem_calloc(size_t u) 49 { 50 auto p = calloc(u, 1); 51 if (!p && u) 52 err_nomem(); 53 return p; 54 } 55 56 @trusted 57 void* mem_realloc(void* p, size_t u) 58 { 59 p = realloc(p, u); 60 if (!p && u) 61 err_nomem(); 62 return p; 63 } 64 65 @trusted 66 void mem_free(void* p) { free(p); } 67 68 extern (C++) 69 { 70 void mem_free_cpp(void *); 71 alias mem_freefp = mem_free_cpp; 72 } 73 74 version (MEM_DEBUG) 75 { 76 alias mem_fstrdup = mem_strdup; 77 alias mem_fcalloc = mem_calloc; 78 alias mem_fmalloc = mem_malloc; 79 alias mem_ffree = mem_free; 80 } 81 else 82 { 83 @trusted 84 char *mem_fstrdup(const(char) *); 85 86 @trusted 87 void *mem_fcalloc(size_t); 88 89 @trusted 90 void *mem_fmalloc(size_t); 91 92 @trusted 93 void mem_ffree(void *) { } 94 }