hare

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

segmalloc.ha (539B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 // Allocates a segment.
      5 fn segmalloc(n: size) nullable *opaque = {
      6 	return match (mmap(null, n, PROT_READ | PROT_WRITE,
      7 		MAP_PRIVATE | MAP_ANON, -1, 0)) {
      8 	case let err: errno =>
      9 		assert(err == ENOMEM: errno);
     10 		yield null;
     11 	case let p: *opaque =>
     12 		yield p;
     13 	};
     14 };
     15 
     16 // Frees a segment allocated with segmalloc.
     17 fn segfree(p: *opaque, s: size) void = {
     18 	match (munmap(p, s)) {
     19 		case let err: errno =>
     20 			abort("munmap failed");
     21 		case void => void;
     22 	};
     23 };