| 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 |
if (n == 0) return null; |
| 8 |
return c_malloc(n); |
| 9 |
}; |
| 10 |
|
| 11 |
// Changes the allocation size of a pointer to n bytes. If n is smaller than |
| 12 |
// the prior allocation, it is truncated; otherwise the allocation is expanded |
| 13 |
// and the values of the new bytes are undefined. May return a different pointer |
| 14 |
// than the one given if there is insufficient space to expand the pointer |
| 15 |
// in-place. Returns null if there is insufficient memory to support the |
| 16 |
// request. |
| 17 |
export fn realloc(p: nullable *opaque, n: size) nullable *opaque = { |
| 18 |
if (n == 0) { |
| 19 |
free(p); |
| 20 |
return null; |
| 21 |
}; |
| 22 |
return c_realloc(p, n); |
| 23 |
}; |
| 24 |
|
| 25 |
// Frees a pointer previously allocated with [[malloc]]. |
| 26 |
export @symbol("rt.free") fn free_(p: nullable *opaque) void = { |
| 27 |
c_free(p); |
| 28 |
}; |
| 29 |
|
| 30 |
@symbol("malloc") fn c_malloc(_: size) nullable *opaque; |
| 31 |
@symbol("realloc") fn c_realloc(_: nullable *opaque, _: size) nullable *opaque; |
| 32 |
@symbol("free") fn c_free(_: nullable *opaque) void; |