diff options
| author | nasr <nsrddyn@gmail.com> | 2025-12-30 21:06:18 +0100 |
|---|---|---|
| committer | nasr <nsrddyn@gmail.com> | 2025-12-30 21:06:18 +0100 |
| commit | 13910acb85367a737617984f9ac8f8bbbfc5281f (patch) | |
| tree | f8a57c11738602d4a078b27cd55600a1ef7248bb | |
| parent | 83be472a172dc152c6a6f69dd24946b785ad370c (diff) | |
checkpoint
| -rw-r--r-- | .gitignore | 3 | ||||
| -rw-r--r-- | .idea/workspace.xml | 102 | ||||
| -rw-r--r-- | base.c | 172 |
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 @@ | |||
| 1 | idea/ | ||
| 2 | idea | ||
| 3 | idea/* | ||
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 | "customColor": "", | ||
| 25 | "associatedIndex": 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 | ||
| @@ -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 | |||
| 13 | enum | ||
| 14 | { | ||
| 15 | BUFFER_SIZE_SMALL = 128, | ||
| 16 | BUFFER_SIZE_DEFAULT = 256, | ||
| 17 | BUFFER_SIZE_LARGE = 512, | ||
| 18 | PATH_MAX_LEN = 4096 | ||
| 19 | }; | ||
| 20 | |||
| 21 | typedef uint64_t u64; | ||
| 22 | typedef uint32_t u32; | ||
| 23 | typedef uint16_t u16; | ||
| 24 | typedef uint8_t u8; | ||
| 25 | |||
| 26 | typedef int8_t i8; | ||
| 27 | typedef int16_t i16; | ||
| 28 | typedef int32_t i32; | ||
| 29 | typedef int64_t i64; | ||
| 30 | |||
| 31 | typedef i16 b16; | ||
| 32 | typedef i32 b32; | ||
| 33 | |||
| 34 | static inline u64 | ||
| 35 | KiB(u64 n) | ||
| 36 | { | ||
| 37 | return n << 10; | ||
| 38 | } | ||
| 39 | static inline u64 | ||
| 40 | MiB(u64 n) | ||
| 41 | { | ||
| 42 | return n << 20; | ||
| 43 | } | ||
| 44 | static inline u64 | ||
| 45 | GiB(u64 n) | ||
| 46 | { | ||
| 47 | return n << 30; | ||
| 48 | } | ||
| 49 | |||
| 50 | typedef struct mem_arena mem_arena; | ||
| 51 | mem_arena * | ||
| 52 | arena_create(u64 capacity); | ||
| 53 | void | ||
| 54 | arena_destroy(mem_arena *arena); | ||
| 55 | void | ||
| 56 | arena_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 | |||
| 79 | struct mem_arena | ||
| 80 | { | ||
| 81 | u64 capacity; | ||
| 82 | u64 pos; | ||
| 83 | }; | ||
| 84 | |||
| 85 | // arena prototypes | ||
| 86 | mem_arena * | ||
| 87 | arena_create(u64 capacity); | ||
| 88 | // make it a void pointer to allow implicit conversion | ||
| 89 | void | ||
| 90 | arena_destroy(mem_arena *arena); | ||
| 91 | |||
| 92 | void * | ||
| 93 | arena_push(mem_arena *arena, u64 size, b32 non_zero); | ||
| 94 | |||
| 95 | void | ||
| 96 | arena_pop(mem_arena *arena, u64 size); | ||
| 97 | |||
| 98 | void | ||
| 99 | arena_pop_to(mem_arena *arena, u64 pos); | ||
| 100 | |||
| 101 | void | ||
| 102 | arena_clear(mem_arena *arena); | ||
| 103 | |||
| 104 | mem_arena * | ||
| 105 | arena_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 | ||
| 121 | void | ||
| 122 | arena_destroy(mem_arena *arena) | ||
| 123 | { | ||
| 124 | munmap(arena, arena->capacity); | ||
| 125 | } | ||
| 126 | |||
| 127 | void * | ||
| 128 | arena_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 | } | ||
| 149 | void | ||
| 150 | arena_pop(mem_arena *arena, u64 size) | ||
| 151 | { | ||
| 152 | size = MIN(size, arena->pos - ARENA_BASE_POS); | ||
| 153 | arena->pos -= size; | ||
| 154 | } | ||
| 155 | |||
| 156 | void | ||
| 157 | arena_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 | |||
| 163 | void | ||
| 164 | arena_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) | ||
