/** * @file memory.c * @brief Memory management implementation for Baba Yaga * @author eli_oat * @version 0.0.1 * @date 2025 * * This file implements memory management utilities for the Baba Yaga language. */ #include #include #include #include "baba_yaga.h" /* ============================================================================ * Memory Management Functions * ============================================================================ */ /* TODO: Implement memory management functions */ void* memory_alloc(size_t size) { void* ptr = malloc(size); if (ptr == NULL) { /* TODO: Handle allocation failure */ fprintf(stderr, "Memory allocation failed: %zu bytes\n", size); } return ptr; } void* memory_realloc(void* ptr, size_t size) { void* new_ptr = realloc(ptr, size); if (new_ptr == NULL) { /* TODO: Handle reallocation failure */ fprintf(stderr, "Memory reallocation failed: %zu bytes\n", size); } return new_ptr; } void memory_free(void* ptr) { if (ptr != NULL) { free(ptr); } } char* memory_strdup(const char* str) { if (str == NULL) { return NULL; } return strdup(str); } char* memory_strndup(const char* str, size_t n) { if (str == NULL) { return NULL; } char* new_str = memory_alloc(n + 1); if (new_str == NULL) { return NULL; } strncpy(new_str, str, n); new_str[n] = '\0'; return new_str; }