Skip to content

Commit 7ea902f

Browse files
authored
Merge pull request #5 from rajamukherji/dev
Dev
2 parents a0ff044 + 827af1e commit 7ea902f

File tree

8 files changed

+319
-162
lines changed

8 files changed

+319
-162
lines changed

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ramp
1+
# Ramp
22
Raja's Attempt at a Memory Pool
33

44
## About
@@ -30,16 +30,26 @@ The `PageSize` argument to `ramp_new` sets the size of each new page.
3030
ramp_t *Ramp = ramp_new(512);
3131
```
3232

33-
During the event handling, any number of memory blocks can be allo0cated using `ramp_alloc(ramp_t *Ramp, size_t Size)`.
33+
During the event handling, any number of memory blocks can be allocated using `ramp_alloc(ramp_t *Ramp, size_t Size)`.
3434

3535
```c
3636
char *Buffer = ramp_alloc(Ramp, 128);
3737
```
3838

39-
Finally, after the event is handled, all of the memory allocated with the `ramp_t` instance can be freed using `ramp_reset(ramp_t *Ramp)`.
39+
Finally, after the event is handled, all of the memory allocated with the `ramp_t` instance can be freed using `ramp_clear(ramp_t *Ramp)`.
4040

4141
```c
42-
ramp_reset(Ramp);
42+
ramp_clear(Ramp);
43+
```
44+
45+
If additional cleanup is required when `ramp_clear()` is called, `ramp_defer(ramp_t *Ramp, size_t Size, void (*CleanupFn)(void *))` can be used instead of `ramp_alloc()`. `CleanupFn()` will be called with the returned pointer when `ramp_clear()` is called.
46+
47+
```c
48+
void object_cleanup(struct object_t *Object) {
49+
// clean up code
50+
}
51+
52+
struct object_t *Object = (object_t *)ramp_defer(Ramp, sizeof(object_t), object_cleanup);
4353
```
4454

4555
The `ramp_t` instance can be reused if required.

build.rabs

Lines changed: 2 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,6 @@
1-
-- ROOT --
1+
:< ROOT >:
22

3-
PLATFORM := defined("PLATFORM") or shell("uname"):trim
4-
OS := defined("OS")
5-
DEBUG := defined("DEBUG")
6-
7-
CFLAGS := ['-g']
8-
LDFLAGS := ['-g']
9-
ARFLAGS := []
10-
PREBUILDS := []
11-
12-
if not DEBUG then
13-
CFLAGS := old + ['-O2']
14-
end
15-
16-
c_compile := fun(Object) do
17-
var Source := Object % "c"
18-
execute('gcc -c {CFLAGS} -o{Object} {Source}')
19-
end
20-
21-
c_includes := fun(Target) do
22-
var Files := []
23-
var Lines := shell('gcc -c {CFLAGS} -M -MG {Target:source}')
24-
var Files := Lines:trim:replace(r"\\\n ", "") / r"[^\\]( +)"
25-
Files:pop
26-
for File in Files do
27-
File := file(File:replace(r"\\ ", " "))
28-
end
29-
return Files
30-
end
31-
32-
SourceTypes := {
33-
"c" is [c_includes, c_compile]
34-
}
35-
36-
c_program := fun(Executable, Objects, Libraries) do
37-
Objects := Objects or []
38-
Libraries := Libraries or []
39-
var Sources := []
40-
for Object in Objects do
41-
for Extension, Functions in SourceTypes do
42-
var Source := Object % Extension
43-
if Source:exists then
44-
Sources:put(Source)
45-
var Scan := Source:scan("INCLUDES", :true)[PREBUILDS] => Functions[1]
46-
Object[Source, Scan] => Functions[2]
47-
exit
48-
end
49-
end
50-
end
51-
Executable[Objects, Libraries] => fun(Executable) do
52-
execute('gcc', '-o', Executable, Objects, Libraries, LDFLAGS)
53-
DEBUG or execute('strip', Executable)
54-
end
55-
DEFAULT[Executable]
56-
end
57-
58-
c_library := fun(Library, Headers, Objects) do
59-
Objects := Objects or []
60-
var Sources := []
61-
for Object in Objects do
62-
for Extension, Functions in SourceTypes do
63-
var Source := Object % Extension
64-
if Source:exists then
65-
Sources:put(Source)
66-
var Scan := Source:scan("INCLUDES", :true)[PREBUILDS] => Functions[1]
67-
Object[Source, Scan] => Functions[2]
68-
exit
69-
end
70-
end
71-
end
72-
Library[Objects] => fun(Library) do
73-
execute('ar rcs', Library, Objects, ARFLAGS)
74-
end
75-
DEFAULT[Library]
76-
for var Header in Headers do
77-
var Target := (INC_DIR / (Header - PATH))[Header] => fun(Target) do
78-
Header:copy(Target)
79-
end
80-
DEFAULT[Target]
81-
end
82-
end
83-
84-
PREFIX := old or PATH
85-
86-
print('PREFIX = {PREFIX}\n')
87-
88-
BIN_DIR := old or (PREFIX / "bin"):mkdir
89-
LIB_DIR := old or (PREFIX / "lib"):mkdir
90-
INC_DIR := old or (PREFIX / "include"):mkdir
3+
%include common.rabs
914

925
vmount("obj", "src")
936
subdir("obj")

common.rabs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
PLATFORM := defined("PLATFORM") or shell("uname"):trim
2+
DEBUG := defined("DEBUG")
3+
4+
CFLAGS := []
5+
LDFLAGS := []
6+
PREBUILDS := []
7+
8+
pkgconfig := fun(Args) do
9+
expr('pkg-config {Args}') => fun() shell('pkg-config', Args):trim
10+
end
11+
12+
c_compile := fun(Source, Object) do
13+
execute('cc -c {CFLAGS} -o{Object} {Source}')
14+
end
15+
16+
c_includes := fun(Target) do
17+
var Lines := shell('cc -c {CFLAGS} -M -MG {Target:source}')
18+
Lines := Lines:replace(r"\\\n ", "")
19+
Lines := Lines[Lines:find(": ") + 2, 0]:trim
20+
var Files := Lines / r"[^\\]( +)"
21+
for File in Files do
22+
File := file(File:replace(r"\\ ", " "))
23+
end
24+
return Files
25+
end
26+
27+
var SourceTypes := {
28+
"c" is [c_includes, c_compile]
29+
}
30+
31+
c_program := fun(Executable, Objects, Libraries) do
32+
Objects := Objects or []
33+
Libraries := Libraries or []
34+
var Sources := []
35+
for Object in Objects do
36+
for Extension, Functions in SourceTypes do
37+
var Source := Object % Extension
38+
if Source:exists then
39+
Sources:put(Source)
40+
var Scan := Source:scan("INCLUDES")[PREBUILDS] => Functions[1]
41+
Object[Source, Scan] => (Functions[2] !! [Source])
42+
exit
43+
end
44+
end
45+
end
46+
Executable[Objects, Libraries] => fun(Executable) do
47+
execute('cc', '-o', Executable, Objects, Libraries, LDFLAGS)
48+
end
49+
DEFAULT[Executable]
50+
ret Executable
51+
end
52+
53+
c_library := fun(Library, Objects, Libraries) do
54+
Objects := Objects or []
55+
Libraries := Libraries or []
56+
var Sources := []
57+
for Object in Objects do
58+
for Extension, Functions in SourceTypes do
59+
var Source := Object % Extension
60+
if Source:exists then
61+
Sources:put(Source)
62+
var Scan := Source:scan("INCLUDES")[PREBUILDS] => Functions[1]
63+
Object[Source, Scan] => (Functions[2] !! [Source])
64+
exit
65+
end
66+
end
67+
end
68+
Library[Objects, Libraries] => fun(Executable) do
69+
execute('ar', 'rcs', Library, Objects, Libraries)
70+
end
71+
DEFAULT[Library]
72+
ret Library
73+
end
74+
75+
PREFIX := old or file('{getenv("DESTDIR") or ""}{defined("PREFIX") or "/usr/local"}')
76+
INSTALL := meta("install")
77+
78+
install := fun(Source, Target, Mode) do
79+
Target[Source] => fun(Target) do
80+
print(Source, " -> ", Target, "\n")
81+
Target:dir:mkdir
82+
Source:copy(Target)
83+
Mode and execute("chmod", Mode, Target)
84+
end
85+
INSTALL[Target]
86+
ret Target
87+
end
88+
89+
TEST := meta("test")
90+

src/build.rabs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
1-
c_library(LIB_DIR / "libramp.a", [file("ramp.h")], [file("ramp.o")])
1+
CFLAGS := old + ["-g"]
2+
LDFLAGS := old + ["-g"]
3+
4+
if defined("DEBUG") then
5+
CFLAGS := old + ["-DDEBUG"]
6+
else
7+
CFLAGS := old + ["-O3"]
8+
end
9+
10+
LIBRAMP := c_library(file("libramp.a"), [file("ramp.o")])
211

312
if defined("TEST_RAMP") then
413
subdir("test")
514
end
15+
16+
var InstallBin := PREFIX / "bin"
17+
var InstallInclude := PREFIX / "include/ramp"
18+
var InstallLib := PREFIX / "lib"
19+
20+
var InstallHeaders := [
21+
"ramp.h"
22+
]
23+
24+
for Header in InstallHeaders do
25+
install(file(Header), InstallInclude / Header)
26+
end
27+
28+
install(file("libramp.a"), InstallLib / "libramp.a")

0 commit comments

Comments
 (0)