File: malloc%2Blibc.ha

package info (click to toggle)
harec 0.24.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,412 kB
  • sloc: ansic: 20,221; asm: 247; makefile: 118; lisp: 80; sh: 45
file content (31 lines) | stat: -rw-r--r-- 1,114 bytes parent folder | download | duplicates (2)
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
29
30
31
// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

// Allocates n bytes of memory and returns a pointer to them, or null if there
// is insufficient memory.
export fn malloc(n: size) nullable *opaque = {
	return c_malloc(n);
};

// Changes the allocation size of a pointer to n bytes. If n is smaller than
// the prior allocation, it is truncated; otherwise the allocation is expanded
// and the values of the new bytes are undefined. May return a different pointer
// than the one given if there is insufficient space to expand the pointer
// in-place. Returns null if there is insufficient memory to support the
// request.
export fn realloc(p: nullable *opaque, n: size) nullable *opaque = {
	if (n == 0) {
		free(p);
		return null;
	};
	return c_realloc(p, n);
};

// Frees a pointer previously allocated with [[malloc]].
export @symbol("rt.free") fn free_(p: nullable *opaque) void = {
	c_free(p);
};

@symbol("malloc") fn c_malloc(size) nullable *opaque;
@symbol("realloc") fn c_realloc(nullable *opaque, size) nullable *opaque;
@symbol("free") fn c_free(nullable *opaque) void;