Initial commit: SUB-OS 64-Bit Production Monolithic Operating System with LazyBox userland suite

This commit is contained in:
subhobhai943 committed 2026-08-16 21:23:52 +05:30
commit cd9c786d14
88 files changed
+7132

No files matched your search

+39
View File
@@ -0,0 +1,39 @@
#include <lib/bitmap.h>
int64_t bitmap_find_first_free(const bitmap_word_t* map, size_t total_bits) {
size_t words = (total_bits + BITS_PER_WORD - 1) / BITS_PER_WORD;
for (size_t w = 0; w < words; w++) {
if (map[w] != (bitmap_word_t)~0ULL) {
for (size_t b = 0; b < BITS_PER_WORD; b++) {
size_t bit_idx = w * BITS_PER_WORD + b;
if (bit_idx >= total_bits) return -1;
if (!bitmap_test(map, bit_idx)) {
return (int64_t)bit_idx;
}
}
}
}
return -1;
}
int64_t bitmap_find_contiguous_free(const bitmap_word_t* map, size_t total_bits, size_t count) {
if (count == 0) return -1;
if (count == 1) return bitmap_find_first_free(map, total_bits);
size_t run = 0;
size_t start = 0;
for (size_t i = 0; i < total_bits; i++) {
if (!bitmap_test(map, i)) {
if (run == 0) start = i;
run++;
if (run == count) {
return (int64_t)start;
}
} else {
run = 0;
}
}
return -1;
}
+226
View File
@@ -0,0 +1,226 @@
#include <lib/string.h>
#include <mm/kmalloc.h>
void* memset(void* dest, int val, size_t count) {
uint8_t* ptr = (uint8_t*)dest;
while (count--) {
*ptr++ = (uint8_t)val;
}
return dest;
}
void* memcpy(void* dest, const void* src, size_t count) {
uint8_t* d = (uint8_t*)dest;
const uint8_t* s = (const uint8_t*)src;
while (count--) {
*d++ = *s++;
}
return dest;
}
void* memmove(void* dest, const void* src, size_t count) {
uint8_t* d = (uint8_t*)dest;
const uint8_t* s = (const uint8_t*)src;
if (d < s) {
while (count--) {
*d++ = *s++;
}
} else {
d += count;
s += count;
while (count--) {
*(--d) = *(--s);
}
}
return dest;
}
int memcmp(const void* ptr1, const void* ptr2, size_t num) {
const uint8_t* p1 = (const uint8_t*)ptr1;
const uint8_t* p2 = (const uint8_t*)ptr2;
while (num--) {
if (*p1 != *p2) {
return *p1 - *p2;
}
p1++;
p2++;
}
return 0;
}
size_t strlen(const char* str) {
size_t len = 0;
while (str && str[len]) {
len++;
}
return len;
}
int strcmp(const char* str1, const char* str2) {
if (!str1 || !str2) return (str1 == str2) ? 0 : (str1 ? 1 : -1);
while (*str1 && (*str1 == *str2)) {
str1++;
str2++;
}
return *(const unsigned char*)str1 - *(const unsigned char*)str2;
}
int strncmp(const char* str1, const char* str2, size_t num) {
if (num == 0) return 0;
while (num > 0 && *str1 && (*str1 == *str2)) {
str1++;
str2++;
num--;
}
if (num == 0) return 0;
return *(const unsigned char*)str1 - *(const unsigned char*)str2;
}
char* strcpy(char* dest, const char* src) {
char* orig_dest = dest;
while ((*dest++ = *src++)) {
}
return orig_dest;
}
char* strncpy(char* dest, const char* src, size_t num) {
size_t i;
for (i = 0; i < num && src[i] != '\0'; i++) {
dest[i] = src[i];
}
for (; i < num; i++) {
dest[i] = '\0';
}
return dest;
}
char* strcat(char* dest, const char* src) {
char* rd = dest;
while (*rd) rd++;
while ((*rd++ = *src++));
return dest;
}
char* strncat(char* dest, const char* src, size_t num) {
char* rd = dest;
while (*rd) rd++;
while (num-- && (*rd++ = *src++));
*rd = '\0';
return dest;
}
char* strchr(const char* str, int c) {
if (!str) return NULL;
while (*str) {
if (*str == (char)c) return (char*)str;
str++;
}
if ((char)c == '\0') return (char*)str;
return NULL;
}
char* strrchr(const char* str, int c) {
if (!str) return NULL;
const char* last = NULL;
while (*str) {
if (*str == (char)c) last = str;
str++;
}
if ((char)c == '\0') return (char*)str;
return (char*)last;
}
char* strstr(const char* haystack, const char* needle) {
if (!haystack || !needle) return NULL;
if (*needle == '\0') return (char*)haystack;
size_t needle_len = strlen(needle);
while (*haystack) {
if (*haystack == *needle && strncmp(haystack, needle, needle_len) == 0) {
return (char*)haystack;
}
haystack++;
}
return NULL;
}
char* strdup(const char* s) {
if (!s) return NULL;
size_t len = strlen(s);
char* copy = (char*)kmalloc(len + 1);
if (copy) {
memcpy(copy, s, len + 1);
}
return copy;
}
static void strreverse(char* begin, char* end) {
char aux;
while (end > begin) {
aux = *end;
*end-- = *begin;
*begin++ = aux;
}
}
char* itoa(int value, char* str, int base) {
if (base < 2 || base > 36) {
*str = '\0';
return str;
}
char* ptr = str, *ptr1 = str;
int tmp_value;
if (value < 0 && base == 10) {
value = -value;
}
do {
tmp_value = value;
value /= base;
*ptr++ = "0123456789abcdef"[tmp_value - value * base];
} while (value);
if (tmp_value < 0 && base == 10) {
*ptr++ = '-';
}
*ptr-- = '\0';
strreverse(ptr1, ptr);
return str;
}
char* itoa_hex(uint64_t value, char* str) {
char* ptr = str;
char* ptr1 = str;
if (value == 0) {
*ptr++ = '0';
*ptr = '\0';
return str;
}
while (value > 0) {
uint8_t mod = value % 16;
*ptr++ = "0123456789ABCDEF"[mod];
value /= 16;
}
*ptr-- = '\0';
strreverse(ptr1, ptr);
return str;
}
int atoi(const char* str) {
int res = 0;
int sign = 1;
while (*str == ' ' || *str == '\t') str++;
if (*str == '-') { sign = -1; str++; }
else if (*str == '+') { str++; }
while (*str >= '0' && *str <= '9') {
res = res * 10 + (*str - '0');
str++;
}
return res * sign;
}
+199
View File
@@ -0,0 +1,199 @@
#include <lib/printf.h>
#include <lib/string.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
static int format_number(char* buf, size_t size, uint64_t num, int base, bool is_signed, bool upper, int width, char pad, bool left_align) {
char temp[65];
int pos = 0;
bool negative = false;
if (is_signed && (int64_t)num < 0) {
negative = true;
num = (uint64_t)(-(int64_t)num);
}
const char* digits = upper ? "0123456789ABCDEF" : "0123456789abcdef";
if (num == 0) {
temp[pos++] = '0';
} else {
while (num > 0) {
temp[pos++] = digits[num % base];
num /= base;
}
}
if (negative) {
temp[pos++] = '-';
}
int total_len = pos;
int pad_count = (width > total_len) ? (width - total_len) : 0;
int written = 0;
// Right-aligned padding
if (!left_align) {
while (pad_count > 0 && (size_t)written < size - 1) {
buf[written++] = pad;
pad_count--;
}
}
// Write number in reverse
for (int i = pos - 1; i >= 0 && (size_t)written < size - 1; i--) {
buf[written++] = temp[i];
}
// Left-aligned padding
if (left_align) {
while (pad_count > 0 && (size_t)written < size - 1) {
buf[written++] = ' ';
pad_count--;
}
}
return written;
}
int vsnprintf(char* buf, size_t size, const char* fmt, va_list args) {
if (!buf || size == 0) return 0;
size_t out_idx = 0;
for (size_t i = 0; fmt[i] != '\0' && out_idx < size - 1; i++) {
if (fmt[i] != '%') {
buf[out_idx++] = fmt[i];
continue;
}
i++; // Skip '%'
if (fmt[i] == '\0') break;
// Flags
bool left_align = false;
if (fmt[i] == '-') {
left_align = true;
i++;
}
char pad = ' ';
if (fmt[i] == '0' && !left_align) {
pad = '0';
i++;
}
// Width
int width = 0;
while (fmt[i] >= '0' && fmt[i] <= '9') {
width = width * 10 + (fmt[i] - '0');
i++;
}
// Length modifiers
bool is_long = false;
bool is_long_long = false;
if (fmt[i] == 'l') {
is_long = true;
i++;
if (fmt[i] == 'l') {
is_long_long = true;
i++;
}
}
switch (fmt[i]) {
case 'c': {
char c = (char)va_arg(args, int);
if (out_idx < size - 1) buf[out_idx++] = c;
break;
}
case 's': {
const char* s = va_arg(args, const char*);
if (!s) s = "(null)";
size_t s_len = strlen(s);
int pad_count = (width > (int)s_len) ? (width - (int)s_len) : 0;
if (!left_align) {
while (pad_count > 0 && out_idx < size - 1) {
buf[out_idx++] = ' ';
pad_count--;
}
}
while (*s && out_idx < size - 1) {
buf[out_idx++] = *s++;
}
if (left_align) {
while (pad_count > 0 && out_idx < size - 1) {
buf[out_idx++] = ' ';
pad_count--;
}
}
break;
}
case 'd':
case 'i': {
int64_t val = is_long_long ? va_arg(args, int64_t) : (is_long ? va_arg(args, long) : va_arg(args, int));
out_idx += format_number(buf + out_idx, size - out_idx, (uint64_t)val, 10, true, false, width, pad, left_align);
break;
}
case 'u': {
uint64_t val = is_long_long ? va_arg(args, uint64_t) : (is_long ? va_arg(args, unsigned long) : va_arg(args, unsigned int));
out_idx += format_number(buf + out_idx, size - out_idx, val, 10, false, false, width, pad, left_align);
break;
}
case 'x': {
uint64_t val = is_long_long ? va_arg(args, uint64_t) : (is_long ? va_arg(args, unsigned long) : va_arg(args, unsigned int));
out_idx += format_number(buf + out_idx, size - out_idx, val, 16, false, false, width, pad, left_align);
break;
}
case 'X': {
uint64_t val = is_long_long ? va_arg(args, uint64_t) : (is_long ? va_arg(args, unsigned long) : va_arg(args, unsigned int));
out_idx += format_number(buf + out_idx, size - out_idx, val, 16, false, true, width, pad, left_align);
break;
}
case 'p': {
uint64_t ptr = (uint64_t)va_arg(args, void*);
if (out_idx < size - 3) {
buf[out_idx++] = '0';
buf[out_idx++] = 'x';
}
out_idx += format_number(buf + out_idx, size - out_idx, ptr, 16, false, false, 16, '0', false);
break;
}
case '%': {
if (out_idx < size - 1) buf[out_idx++] = '%';
break;
}
default:
if (out_idx < size - 1) buf[out_idx++] = fmt[i];
break;
}
}
buf[out_idx] = '\0';
return (int)out_idx;
}
int sprintf(char* buf, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
int ret = vsnprintf(buf, 4096, fmt, args);
va_end(args);
return ret;
}
int snprintf(char* buf, size_t size, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
int ret = vsnprintf(buf, size, fmt, args);
va_end(args);
return ret;
}
int vsprintf(char* buf, const char* fmt, va_list args) {
return vsnprintf(buf, 4096, fmt, args);
}