sprintf_malloc

This commit is contained in:
Timerix 2024-11-16 19:36:17 +05:00
parent b8296a45a7
commit 83172df776
7 changed files with 41 additions and 12 deletions

View File

@ -6,6 +6,18 @@ void VM_init(VM* vm){
vm->state = VMState_Initialized;
}
void VM_setErrorMessage(VM* vm, const char* format, ...){
va_list argv;
va_start(argv, format);
char* NULLABLE(buf) = vsprintf_malloc(256, format, argv);
va_end(argv);
if(buf == NULL){
buf = malloc(16);
strcpy(buf, "SPRINTF FAILED");
}
vm->error_message = buf;
}
bool VM_loadProgram(VM* vm, u8* data, size_t size){
if(data == NULL){
VM_setErrorMessage(vm, "[VM_loadProgram] can't load program because data == NULL");
@ -33,7 +45,7 @@ i32 VM_executeProgram(VM* vm){
const Instruction* instr = Instruction_getFromOpcode(opcode);
if(instr == NULL){
VM_setErrorMessage(vm, "[%p] unknown opcode %02x", (void*)pos, opcode);
return -1;
}

View File

@ -45,7 +45,7 @@ typedef struct VM {
};
VMState state;
NULLABLE(char* error_message);
char* NULLABLE(error_message);
u8* data;
size_t data_size;
@ -62,10 +62,6 @@ bool VM_loadProgram(VM* vm, u8* data, size_t size);
/// @return program exit code or -1 on error (check vm.error_message)
i32 VM_executeProgram(VM* vm);
#define VM_setErrorMessage(V, FORMAT, ...) {\
char* buf = malloc(256);\
sprintf(buf, FORMAT, ##__VA_ARGS__);\
vm->error_message = buf;\
}
bool VM_dataRead(VM* vm, void* dst, size_t pos, size_t size);
void VM_setErrorMessage(VM* vm, const char* format, ...) __attribute__((__format__ (__printf__, 2, 3)));

View File

@ -1,6 +1,6 @@
#include "impl_macros.h"
NULLABLE(FILE* fileFromN(VM* vm, size_t pos, u32 file_n)){
FILE* NULLABLE(fileFromN)(VM* vm, size_t pos, u32 file_n){
FILE* f = NULL;
switch(file_n){
case 0: f = stdin; break;

View File

@ -16,7 +16,7 @@ const Instruction instructions[] = {
};
const size_t instructions_count = sizeof(instructions)/sizeof(instructions[0]);
NULLABLE(const Instruction* Instruction_getFromOpcode(u8 opcode)){
const Instruction* NULLABLE(Instruction_getFromOpcode)(u8 opcode){
if(opcode >= instructions_count)
return NULL;

View File

@ -18,7 +18,7 @@ typedef struct Instruction {
/// @brief get instruction info from table
/// @param opcode any byte
/// @return ptr to struct or NULL
NULLABLE(const Instruction* Instruction_getFromOpcode(u8 opcode));
const Instruction* NULLABLE(Instruction_getFromOpcode)(u8 opcode);
i32 NOP_impl(VM* vm, size_t pos);
i32 PUSH_impl(VM* vm, size_t pos);

View File

@ -75,3 +75,21 @@ i32 main(const i32 argc, const char** argv){
free(buffer);
return exit_code;
}
char* NULLABLE(sprintf_malloc)(size_t buffer_size, const char* format, ...){
va_list argv;
va_start(argv, format);
char* NULLABLE(heap_ptr) = vsprintf_malloc(buffer_size, format, argv);
va_end(argv);
return heap_ptr;
}
char* NULLABLE(vsprintf_malloc)(size_t buffer_size, const char* format, va_list argv){
char* buf = malloc(buffer_size);
int r = vsprintf_s(buf, buffer_size, format, argv);
if(r < 0){
free(buf);
return NULL;
}
return buf;
}

View File

@ -27,4 +27,7 @@ typedef u8 bool;
#define printfe(FORMAT, ...) fprintf(stderr, FORMAT ,##__VA_ARGS__)
/// @warning pointer can be null
#define NULLABLE(DECLARATION) DECLARATION
#define NULLABLE(NAME) NAME
char* NULLABLE(sprintf_malloc)(size_t buffer_size, const char* format, ...) __attribute__((__format__ (__printf__, 2, 3)));
char* NULLABLE(vsprintf_malloc)(size_t buffer_size, const char* format, va_list argv);