aboutsummaryrefslogtreecommitdiff
path: root/code/lib/memory.felan
blob: 0082a1ea433551376c95e0fe7c163c3d2b6f1715 (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
25
26
27
28
29
@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);
memcpy :: @c_function(libc,"memcpy",(*void,*void,i64)->(*void));

malloc :: (size:u64,comptime t:type) -> (*t) {
  return @cast(malloc(@cast(size*@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));
};

memcpy :: (dest:*anytype,src:@type_of(dest),n:u64) -> void {
  memcpy(@cast(dest,*void),@cast(src,*void),@cast(n,i64));
};