hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

malloc+libc.ha (1114B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 // Allocates n bytes of memory and returns a pointer to them, or null if there
      5 // is insufficient memory.
      6 export fn malloc(n: size) nullable *opaque = {
      7 	return c_malloc(n);
      8 };
      9 
     10 // Changes the allocation size of a pointer to n bytes. If n is smaller than
     11 // the prior allocation, it is truncated; otherwise the allocation is expanded
     12 // and the values of the new bytes are undefined. May return a different pointer
     13 // than the one given if there is insufficient space to expand the pointer
     14 // in-place. Returns null if there is insufficient memory to support the
     15 // request.
     16 export fn realloc(p: nullable *opaque, n: size) nullable *opaque = {
     17 	if (n == 0) {
     18 		free(p);
     19 		return null;
     20 	};
     21 	return c_realloc(p, n);
     22 };
     23 
     24 // Frees a pointer previously allocated with [[malloc]].
     25 export @symbol("rt.free") fn free_(p: nullable *opaque) void = {
     26 	c_free(p);
     27 };
     28 
     29 @symbol("malloc") fn c_malloc(size) nullable *opaque;
     30 @symbol("realloc") fn c_realloc(nullable *opaque, size) nullable *opaque;
     31 @symbol("free") fn c_free(nullable *opaque) void;