summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornasr <nsrddyn@gmail.com>2025-12-30 21:06:18 +0100
committernasr <nsrddyn@gmail.com>2025-12-30 21:06:18 +0100
commit13910acb85367a737617984f9ac8f8bbbfc5281f (patch)
treef8a57c11738602d4a078b27cd55600a1ef7248bb
parent83be472a172dc152c6a6f69dd24946b785ad370c (diff)
checkpoint
-rw-r--r--.gitignore3
-rw-r--r--.idea/workspace.xml102
-rw-r--r--base.c172
3 files changed, 105 insertions, 172 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..ec5545e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
1idea/
2idea
3idea/*
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
new file mode 100644
index 0000000..47a4672
--- /dev/null
+++ b/.idea/workspace.xml
@@ -0,0 +1,102 @@
1<?xml version="1.0" encoding="UTF-8"?>
2<project version="4">
3 <component name="AutoImportSettings">
4 <option name="autoReloadType" value="SELECTIVE" />
5 </component>
6 <component name="BackendCodeEditorMiscSettings">
7 <option name="/Default/Environment/Hierarchy/GeneratedFilesCacheKey/Timestamp/@EntryValue" value="4" type="long" />
8 <option name="/Default/Housekeeping/FeatureSuggestion/FeatureSuggestionManager/DisabledSuggesters/=SwitchToGoToActionSuggester/@EntryIndexedValue" value="true" type="bool" />
9 </component>
10 <component name="ChangeListManager">
11 <list default="true" id="4e2e747b-4cf7-4326-a0ab-4428fecfc88f" name="Changes" comment="" />
12 <option name="SHOW_DIALOG" value="false" />
13 <option name="HIGHLIGHT_CONFLICTS" value="true" />
14 <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
15 <option name="LAST_RESOLUTION" value="IGNORE" />
16 </component>
17 <component name="Git.Settings">
18 <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
19 </component>
20 <component name="MacroExpansionManager">
21 <option name="directoryName" value="tzFWBH8r" />
22 </component>
23 <component name="ProjectColorInfo">{
24 &quot;customColor&quot;: &quot;&quot;,
25 &quot;associatedIndex&quot;: 4
26}</component>
27 <component name="ProjectId" id="37WXJwlfHgfbTUSQajVrapU6YHf" />
28 <component name="ProjectViewState">
29 <option name="autoscrollToSource" value="true" />
30 <option name="hideEmptyMiddlePackages" value="true" />
31 <option name="showLibraryContents" value="true" />
32 </component>
33 <component name="PropertiesComponent"><![CDATA[{
34 "keyToString": {
35 "C/C++ File.main.c.executor": "Debug",
36 "C/C++ File.main.cpp.executor": "Debug",
37 "ModuleVcsDetector.initialDetectionPerformed": "true",
38 "RunOnceActivity.RadMigrateCodeStyle": "true",
39 "RunOnceActivity.ShowReadmeOnStart": "true",
40 "RunOnceActivity.cidr.known.project.marker": "true",
41 "RunOnceActivity.readMode.enableVisualFormatting": "true",
42 "RunOnceActivity.typescript.service.memoryLimit.init": "true",
43 "cf.advertisement.text.overridden": "true",
44 "cf.first.check.clang-format": "false",
45 "cidr.known.project.marker": "true",
46 "git-widget-placeholder": "main",
47 "nodejs_package_manager_path": "npm",
48 "org.rust.first.attach.projects": "true",
49 "settings.editor.selected.configurable": "preferences.lookFeel",
50 "vue.rearranger.settings.migration": "true"
51 }
52}]]></component>
53 <component name="RunManager" selected="C/C++ File.main.c">
54 <configuration name="main.c" type="CppFileRunConfiguration" factoryName="CppFileRunConfiguration" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="x11-programming" TARGET_NAME="main.c" CONFIG_NAME="main.c">
55 <option name="compilerOptions" value="-lX11" />
56 <option name="sourceFile" value="main.c" />
57 <method v="2">
58 <option name="com.jetbrains.cidr.cpp.runfile.CppFileBuildBeforeRunTaskProvider$BasicBuildBeforeRunTask" enabled="true" />
59 </method>
60 </configuration>
61 <configuration name="main.cpp" type="CppFileRunConfiguration" factoryName="CppFileRunConfiguration" nameIsGenerated="true" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="x11-programming" TARGET_NAME="main.cpp" CONFIG_NAME="main.cpp">
62 <option name="compilerOptions" value="-lX11" />
63 <option name="sourceFile" value="main.cpp" />
64 <method v="2">
65 <option name="com.jetbrains.cidr.cpp.runfile.CppFileBuildBeforeRunTaskProvider$BasicBuildBeforeRunTask" enabled="true" />
66 </method>
67 </configuration>
68 <list>
69 <item itemvalue="C/C++ File.main.c" />
70 <item itemvalue="C/C++ File.main.cpp" />
71 </list>
72 </component>
73 <component name="RustProjectSettings">
74 <option name="toolchainHomeDirectory" value="/usr/bin" />
75 </component>
76 <component name="TaskManager">
77 <task active="true" id="Default" summary="Default task">
78 <changelist id="4e2e747b-4cf7-4326-a0ab-4428fecfc88f" name="Changes" comment="" />
79 <created>1767019734019</created>
80 <option name="number" value="Default" />
81 <option name="presentableId" value="Default" />
82 <updated>1767019734019</updated>
83 <workItem from="1767019735790" duration="556000" />
84 <workItem from="1767037529556" duration="1209000" />
85 <workItem from="1767098248582" duration="514000" />
86 <workItem from="1767099177012" duration="1625000" />
87 <workItem from="1767120425134" duration="4461000" />
88 </task>
89 <servers />
90 </component>
91 <component name="TypeScriptGeneratedFilesManager">
92 <option name="version" value="3" />
93 </component>
94 <component name="XDebuggerManager">
95 <watches-manager>
96 <configuration name="CppFileRunConfiguration">
97 <watch expression="gc" language="C++" />
98 <watch expression="color" language="C++" />
99 </configuration>
100 </watches-manager>
101 </component>
102</project> \ No newline at end of file
diff --git a/base.c b/base.c
deleted file mode 100644
index 519e644..0000000
--- a/base.c
+++ /dev/null
@@ -1,172 +0,0 @@
1#include <sys/mman.h>
2#include <stddef.h>
3#include <stdint.h>
4#include <string.h>
5#include <assert.h>
6
7#define OK 0
8#define ERR_IO 1
9#define ERR_PARSE 2
10#define ERR_PERM 3
11#define ERR_INVALID 4
12
13enum
14{
15 BUFFER_SIZE_SMALL = 128,
16 BUFFER_SIZE_DEFAULT = 256,
17 BUFFER_SIZE_LARGE = 512,
18 PATH_MAX_LEN = 4096
19};
20
21typedef uint64_t u64;
22typedef uint32_t u32;
23typedef uint16_t u16;
24typedef uint8_t u8;
25
26typedef int8_t i8;
27typedef int16_t i16;
28typedef int32_t i32;
29typedef int64_t i64;
30
31typedef i16 b16;
32typedef i32 b32;
33
34static inline u64
35KiB(u64 n)
36{
37 return n << 10;
38}
39static inline u64
40MiB(u64 n)
41{
42 return n << 20;
43}
44static inline u64
45GiB(u64 n)
46{
47 return n << 30;
48}
49
50typedef struct mem_arena mem_arena;
51mem_arena *
52arena_create(u64 capacity);
53void
54arena_destroy(mem_arena *arena);
55void
56arena_clear(mem_arena *arena);
57
58/**
59 * Arena Helper macro's
60 * */
61
62#define MIN(a, b) (((a) < (b)) ? (a) : (b))
63#define MAX(a, b) (((a) > (b)) ? (a) : (b))
64#define ALIGN_UP_POW2(n, p) (((u64)(n) + ((u64)(p) - 1)) & (~((u64)(p) - 1)))
65
66/*
67 * Represents a disk partition with major/minor device numbers and block count.
68 */
69
70/**
71 * replacing malloc/free with arena allocaters
72 *
73 * */
74
75#define ARENA_BASE_POS (sizeof(mem_arena))
76// void * for the size of a pointer on the machine, 64/32bit comp
77#define ARENA_ALIGN (sizeof(void *))
78
79struct mem_arena
80{
81 u64 capacity;
82 u64 pos;
83};
84
85// arena prototypes
86mem_arena *
87arena_create(u64 capacity);
88// make it a void pointer to allow implicit conversion
89void
90arena_destroy(mem_arena *arena);
91
92void *
93arena_push(mem_arena *arena, u64 size, b32 non_zero);
94
95void
96arena_pop(mem_arena *arena, u64 size);
97
98void
99arena_pop_to(mem_arena *arena, u64 pos);
100
101void
102arena_clear(mem_arena *arena);
103
104mem_arena *
105arena_create(u64 capacity)
106{
107 mem_arena *arena = mmap(0, capacity, PROT_READ | PROT_WRITE | PROT_EXEC,
108 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
109 if (arena == MAP_FAILED)
110 {
111 assert(0);
112 }
113
114 arena->capacity = capacity;
115 arena->pos = ARENA_BASE_POS;
116
117 return arena;
118}
119
120// make it a void pointer to allow implicit conversion
121void
122arena_destroy(mem_arena *arena)
123{
124 munmap(arena, arena->capacity);
125}
126
127void *
128arena_push(mem_arena *arena, u64 size, b32 non_zero)
129{
130 u64 pos_aligned = ALIGN_UP_POW2(arena->pos, ARENA_ALIGN);
131 u64 new_pos = pos_aligned + size;
132
133 if (new_pos > arena->capacity)
134 {
135 assert(0);
136 return NULL;
137 }
138
139 arena->pos = new_pos;
140 // cast to u8 to be able to do pointer arithemtic
141 u8 *out = (u8 *)arena + pos_aligned;
142
143 if (!non_zero)
144 {
145 memset(out, 0, size);
146 }
147 return out;
148}
149void
150arena_pop(mem_arena *arena, u64 size)
151{
152 size = MIN(size, arena->pos - ARENA_BASE_POS);
153 arena->pos -= size;
154}
155
156void
157arena_pop_to(mem_arena *arena, u64 pos)
158{
159 u64 size = pos < arena->pos ? arena->pos - pos : 0;
160 arena_pop(arena, size);
161}
162
163void
164arena_clear(mem_arena *arena)
165{
166 arena_pop_to(arena, ARENA_BASE_POS);
167}
168
169#define PUSH_STRUCT(arena, T) (T *)arena_push((arena), sizeof(T), 0)
170#define PUSH_STRUCT_NZ(arena, T) (T *)arena_push((arena), sizeof(T), 1)
171#define PUSH_ARRAY(arena, T, n) (T *)arena_push((arena), sizeof(T) * (n), 0)
172#define PUSH_ARRAY_NZ(arena, T, n) (T *)arena_push((arena), sizeof(T) * (n), 1)