aboutsummaryrefslogtreecommitdiff
path: root/code/lib/memory.felan
blob: 271068551fef4144822baed2b3d885917a111335 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@import("operator.felan");

libc :: @c_library("/usr/lib/libc.so.6");
malloc :: @c_function(libc,"malloc",(i64)->(*void));
realloc :: @c_function(libc,"realloc",(*void,i64)->(*void));
free :: @c_function(libc,"free",(*void)->void);

malloc :: (size:i64,comptime t:type) -> (*t) {
  return @cast(malloc(size*@cast(@size_of(t),i64)),*t);
};

realloc :: (ptr:*anytype,size:u64) -> (@type_of(ptr)) {
  return @cast(
    realloc(
      @cast(ptr,*void),
      @cast(size*@size_of(@type_of(ptr.*)),i64),
    ),
    @type_of(ptr)
  );
};

free :: (ptr:*anytype) -> void {
  free(@cast(ptr,*void));
};