Skip to content

Commit

Permalink
Merge pull request #5 from rajamukherji/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
rajamukherji authored Jun 25, 2020
2 parents a0ff044 + 827af1e commit 7ea902f
Show file tree
Hide file tree
Showing 8 changed files with 319 additions and 162 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ramp
# Ramp
Raja's Attempt at a Memory Pool

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

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

```c
char *Buffer = ramp_alloc(Ramp, 128);
```

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)`.
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)`.

```c
ramp_reset(Ramp);
ramp_clear(Ramp);
```
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.
```c
void object_cleanup(struct object_t *Object) {
// clean up code
}
struct object_t *Object = (object_t *)ramp_defer(Ramp, sizeof(object_t), object_cleanup);
```

The `ramp_t` instance can be reused if required.
Expand Down
91 changes: 2 additions & 89 deletions build.rabs
Original file line number Diff line number Diff line change
@@ -1,93 +1,6 @@
-- ROOT --
:< ROOT >:

PLATFORM := defined("PLATFORM") or shell("uname"):trim
OS := defined("OS")
DEBUG := defined("DEBUG")

CFLAGS := ['-g']
LDFLAGS := ['-g']
ARFLAGS := []
PREBUILDS := []

if not DEBUG then
CFLAGS := old + ['-O2']
end

c_compile := fun(Object) do
var Source := Object % "c"
execute('gcc -c {CFLAGS} -o{Object} {Source}')
end

c_includes := fun(Target) do
var Files := []
var Lines := shell('gcc -c {CFLAGS} -M -MG {Target:source}')
var Files := Lines:trim:replace(r"\\\n ", "") / r"[^\\]( +)"
Files:pop
for File in Files do
File := file(File:replace(r"\\ ", " "))
end
return Files
end

SourceTypes := {
"c" is [c_includes, c_compile]
}

c_program := fun(Executable, Objects, Libraries) do
Objects := Objects or []
Libraries := Libraries or []
var Sources := []
for Object in Objects do
for Extension, Functions in SourceTypes do
var Source := Object % Extension
if Source:exists then
Sources:put(Source)
var Scan := Source:scan("INCLUDES", :true)[PREBUILDS] => Functions[1]
Object[Source, Scan] => Functions[2]
exit
end
end
end
Executable[Objects, Libraries] => fun(Executable) do
execute('gcc', '-o', Executable, Objects, Libraries, LDFLAGS)
DEBUG or execute('strip', Executable)
end
DEFAULT[Executable]
end

c_library := fun(Library, Headers, Objects) do
Objects := Objects or []
var Sources := []
for Object in Objects do
for Extension, Functions in SourceTypes do
var Source := Object % Extension
if Source:exists then
Sources:put(Source)
var Scan := Source:scan("INCLUDES", :true)[PREBUILDS] => Functions[1]
Object[Source, Scan] => Functions[2]
exit
end
end
end
Library[Objects] => fun(Library) do
execute('ar rcs', Library, Objects, ARFLAGS)
end
DEFAULT[Library]
for var Header in Headers do
var Target := (INC_DIR / (Header - PATH))[Header] => fun(Target) do
Header:copy(Target)
end
DEFAULT[Target]
end
end

PREFIX := old or PATH

print('PREFIX = {PREFIX}\n')

BIN_DIR := old or (PREFIX / "bin"):mkdir
LIB_DIR := old or (PREFIX / "lib"):mkdir
INC_DIR := old or (PREFIX / "include"):mkdir
%include common.rabs

vmount("obj", "src")
subdir("obj")
90 changes: 90 additions & 0 deletions common.rabs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
PLATFORM := defined("PLATFORM") or shell("uname"):trim
DEBUG := defined("DEBUG")

CFLAGS := []
LDFLAGS := []
PREBUILDS := []

pkgconfig := fun(Args) do
expr('pkg-config {Args}') => fun() shell('pkg-config', Args):trim
end

c_compile := fun(Source, Object) do
execute('cc -c {CFLAGS} -o{Object} {Source}')
end

c_includes := fun(Target) do
var Lines := shell('cc -c {CFLAGS} -M -MG {Target:source}')
Lines := Lines:replace(r"\\\n ", "")
Lines := Lines[Lines:find(": ") + 2, 0]:trim
var Files := Lines / r"[^\\]( +)"
for File in Files do
File := file(File:replace(r"\\ ", " "))
end
return Files
end

var SourceTypes := {
"c" is [c_includes, c_compile]
}

c_program := fun(Executable, Objects, Libraries) do
Objects := Objects or []
Libraries := Libraries or []
var Sources := []
for Object in Objects do
for Extension, Functions in SourceTypes do
var Source := Object % Extension
if Source:exists then
Sources:put(Source)
var Scan := Source:scan("INCLUDES")[PREBUILDS] => Functions[1]
Object[Source, Scan] => (Functions[2] !! [Source])
exit
end
end
end
Executable[Objects, Libraries] => fun(Executable) do
execute('cc', '-o', Executable, Objects, Libraries, LDFLAGS)
end
DEFAULT[Executable]
ret Executable
end

c_library := fun(Library, Objects, Libraries) do
Objects := Objects or []
Libraries := Libraries or []
var Sources := []
for Object in Objects do
for Extension, Functions in SourceTypes do
var Source := Object % Extension
if Source:exists then
Sources:put(Source)
var Scan := Source:scan("INCLUDES")[PREBUILDS] => Functions[1]
Object[Source, Scan] => (Functions[2] !! [Source])
exit
end
end
end
Library[Objects, Libraries] => fun(Executable) do
execute('ar', 'rcs', Library, Objects, Libraries)
end
DEFAULT[Library]
ret Library
end

PREFIX := old or file('{getenv("DESTDIR") or ""}{defined("PREFIX") or "/usr/local"}')
INSTALL := meta("install")

install := fun(Source, Target, Mode) do
Target[Source] => fun(Target) do
print(Source, " -> ", Target, "\n")
Target:dir:mkdir
Source:copy(Target)
Mode and execute("chmod", Mode, Target)
end
INSTALL[Target]
ret Target
end

TEST := meta("test")

25 changes: 24 additions & 1 deletion src/build.rabs
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
c_library(LIB_DIR / "libramp.a", [file("ramp.h")], [file("ramp.o")])
CFLAGS := old + ["-g"]
LDFLAGS := old + ["-g"]

if defined("DEBUG") then
CFLAGS := old + ["-DDEBUG"]
else
CFLAGS := old + ["-O3"]
end

LIBRAMP := c_library(file("libramp.a"), [file("ramp.o")])

if defined("TEST_RAMP") then
subdir("test")
end

var InstallBin := PREFIX / "bin"
var InstallInclude := PREFIX / "include/ramp"
var InstallLib := PREFIX / "lib"

var InstallHeaders := [
"ramp.h"
]

for Header in InstallHeaders do
install(file(Header), InstallInclude / Header)
end

install(file("libramp.a"), InstallLib / "libramp.a")
Loading

0 comments on commit 7ea902f

Please sign in to comment.