Skip to content

ALightbolt4G/CM

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

1 Commit
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš€ CM Framework

A modern, enterprise-grade backend framework written in pure C99.
CM gives you the elegance of Express.js, the performance of raw C, and the safety of a built-in garbage collector โ€” all in one lightweight library.


โœจ Features

Module Description
๐Ÿง  Memory / GC Automatic garbage collection with ref-counting, mark-and-sweep, and live stats
๐Ÿ”’ Thread Safety OS-agnostic CMMutex and CMThread โ€” wraps Win32 & POSIX transparently
๐ŸŒ HTTP Server Express.js-style router with cm_app_get() / cm_app_post()
๐Ÿ“„ File I/O GC-tracked file reading and writing via cm_file_read() / cm_file_write()
๐Ÿ—บ๏ธ Hash Maps High-performance cm_map_t with automatic resizing and load-factor control
๐Ÿ”ค Dynamic Strings Safe unicode-aware cm_string_t with full formatting and slicing API
๐Ÿ“ฆ JSON Full JSON parse + stringify โ€” no third-party dependencies
๐Ÿ’พ Map Persistence Serialize / deserialize maps to JSON files in one line
๐ŸŒ Unicode / UTF-8 Native emoji and international character support on all platforms

๐Ÿ“ Project Structure

CM/
โ”œโ”€โ”€ include/cm/        # Public headers
โ”‚   โ”œโ”€โ”€ core.h         # Version, base types, macros
โ”‚   โ”œโ”€โ”€ memory.h       # GC allocator API
โ”‚   โ”œโ”€โ”€ string.h       # Dynamic string API
โ”‚   โ”œโ”€โ”€ array.h        # Dynamic array API
โ”‚   โ”œโ”€โ”€ map.h          # Hash map API + JSON persistence
โ”‚   โ”œโ”€โ”€ json.h         # JSON parse & stringify
โ”‚   โ”œโ”€โ”€ http.h         # HTTP server & router
โ”‚   โ”œโ”€โ”€ file.h         # File I/O API
โ”‚   โ”œโ”€โ”€ thread.h       # OS-agnostic threading API
โ”‚   โ””โ”€โ”€ error.h        # Error handling & signal detection
โ”œโ”€โ”€ src/               # Module implementations
โ”œโ”€โ”€ tests/             # Unit test suite (CTest)
โ”œโ”€โ”€ public_html/       # Static frontend (HTML/CSS/JS)
โ”‚   โ”œโ”€โ”€ index.html     # Login + Calculator SPA
โ”‚   โ”œโ”€โ”€ style.css      # Glassmorphism dark UI
โ”‚   โ””โ”€โ”€ script.js      # Calculator frontend logic
โ”œโ”€โ”€ main.c             # Example server entry point
โ””โ”€โ”€ CMakeLists.txt     # Build system

โšก Quick Start

1. Build the Library

Requirements: CMake 3.10+, GCC / MinGW-w64

cmake -B build
cmake --build build

2. Run the Example Server

cd build
./cm_server.exe        # Windows
./cm_server            # Linux

Then open http://localhost:8080 in your browser.
Login with admin / 1234 to access the live Calculator.


๐Ÿงช Running Tests

cd build
ctest --output-on-failure

๐Ÿ“– API Reference

๐Ÿง  Memory & Garbage Collector

cm_gc_init();                  // Initialize GC (call once on startup)
void* p = cm_alloc(size, "T"); // Allocate GC-tracked memory
cm_free(p);                    // Decrement ref count / free
cm_retain(p);                  // Increment ref count
cm_gc_collect();               // Manually trigger sweep
cm_gc_stats();                 // Print memory statistics to console
cm_gc_shutdown();              // Final sweep + shutdown

๐Ÿ”ค Strings

cm_string_t* s = cm_string_new("Hello!");
cm_string_t* f = cm_string_format("v%s", "5.0.0");
cm_string_length(s);           // UTF-8 aware length
cm_string_free(s);

๐Ÿ—บ๏ธ Hash Maps

cm_map_t* m = cm_map_new();
cm_map_set(m, "key", "value", 6);
char* v = (char*)cm_map_get(m, "key");
cm_map_save_to_json(m, "config.json");   // Persist to disk
cm_map_t* m2 = cm_map_load_from_json("config.json");  // Load from disk
cm_map_free(m);

๐ŸŒ HTTP Server (Express-style)

void home(CMHttpRequest* req, CMHttpResponse* res) {
    cm_res_send(res, "Hello from CM!");
}

void api_data(CMHttpRequest* req, CMHttpResponse* res) {
    struct CMJsonNode* node = cm_json_parse("{\"ok\":true}");
    cm_res_json(res, node);
    CMJsonNode_delete(node);
}

int main() {
    cm_gc_init();

    cm_app_get("/",          home);
    cm_app_get("/style.css", serve_css);
    cm_app_post("/api/data", api_data);

    cm_app_listen(8080);
    cm_gc_shutdown();
}

๐Ÿ“„ File I/O

cm_file_write("out.txt", "Hello, CM!");
cm_string_t* content = cm_file_read("out.txt");
printf("%s\n", content->data);
cm_string_free(content);

๐Ÿ”’ Threading

void* my_worker(void* arg) {
    printf("Running in thread!\n");
    return NULL;
}

CMThread t = cm_thread_create(my_worker, NULL);
cm_thread_join(t);

CMMutex lock = cm_mutex_init();
cm_mutex_lock(lock);
// ... critical section ...
cm_mutex_unlock(lock);

๐Ÿ“ฆ JSON

struct CMJsonNode* root = cm_json_parse("{\"name\":\"CM\",\"version\":5}");
cm_string_t* str = cm_json_stringify(root);
printf("%s\n", str->data);
cm_string_free(str);
CMJsonNode_delete(root);

๐ŸŽจ Included Frontend Demo

CM ships with a glassmorphism dark-mode web app served entirely from C:

  • ๐Ÿ” Login Screen โ€” JWT-free session with backend auth via POST /api/login
  • ๐Ÿงฎ Calculator UI โ€” Fully functional, logs every calculation to the server
  • ๐Ÿ“Š Live GC Stats โ€” Memory stats printed to console on every calculation

๐Ÿ—๏ธ Architecture

Request โ”€โ†’ TCP Socket โ”€โ†’ HTTP Parser โ”€โ†’ Router
                                          โ”‚
                            โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                            โ–ผ             โ–ผ             โ–ผ
                        Static File   JSON API    Log + GC Stats
                        (File I/O)   (Map/JSON)   (cm_gc_stats)

๐Ÿ“Š Memory Safety Guarantee

The CM GC ensures zero unbounded leaks under normal usage:

โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
              GARBAGE COLLECTOR STATISTICS
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
  Total objects    โ”‚                   39
  Total memory     โ”‚                 1524 bytes
  Peak memory      โ”‚                13058 bytes
  Allocations      โ”‚                  214
  Frees            โ”‚                  175
  Collections      โ”‚                    5
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

๐Ÿ—บ๏ธ Roadmap

  • HTTPS / TLS support via cm_tls.h
  • WebSocket upgrade protocol
  • Route middleware / pipeline hooks
  • JWT authentication helper
  • Database adapter interface

๐Ÿ“„ License

MIT License โ€” Copyright ยฉ 2026 Adham Hossam


Built with โค๏ธ and pure C99 โ€” no dependencies, no runtime, no compromise.

About

A personal experimental C library exploring GC, dynamic structures, and exception handling.

Resources

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors