Skip to content

Commit d8f2168

Browse files
committed
New Memory management + Fixed bugs
1 parent c519a7a commit d8f2168

File tree

3 files changed

+61
-28
lines changed

3 files changed

+61
-28
lines changed

main.c

+23-18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <stdio.h>
55
#include <stdlib.h>
66
#include <string.h>
7+
#include <errno.h>
78

89
#include "utils.h"
910

@@ -13,38 +14,40 @@
1314
* \param _file The file to open
1415
* \param _prog Return value for the Brainfuck code string
1516
* \param _len Return value for length of the Brainfuck code string
16-
* \return Error 1, Success 0
17+
* \return 0 = Success, 1 = Err
1718
*/
1819
int loadProgr(const char* _file, char** _prog, size_t* _len);
1920

2021
Command cmds[] = {
21-
// Informatical args like help or version come before all other to quit the program before making anything else
2222
{"yacbfi", 0, COMMAND_NO_ARG, "[options]", 0},
2323
{"help", 'h', COMMAND_NO_ARG, "Shows this message", 'h'},
2424
{"version", 'v', COMMAND_NO_ARG, "Show the current version number", 'v'},
2525
{"file", 'f', COMMAND_HAS_ARG, "Brainf*ck File to use", 'f'},
2626
{NULL, 0, 0, NULL, 0}
2727
};
2828

29-
#define ALLOC_MEM (size_t)5
29+
#define ALLOC_MEM (size_t)512
3030
#define REALLOC_MEM (size_t)1024
3131

3232
int main(int argc, char const *argv[]) {
3333
const char* filename;
3434
char* program;
3535
size_t program_len;
3636

37-
size_t memory_size = ALLOC_MEM;
38-
unsigned char* memory = (char*) calloc(sizeof(unsigned char), memory_size); ERR_ALLOC(memory)
37+
size_t memory_length = ALLOC_MEM;
38+
unsigned char* memory = (char*) calloc(memory_length, sizeof(unsigned char));
39+
if (memory == NULL) { ERR_ALLOC(); }
40+
3941
size_t mem_ptr = 0;
40-
unsigned char current_instr;
4142

4243
int err = handleArgs(argc, argv, cmds, &filename);
43-
if (err == 1) { ERR("Failed to Initialize!\n") }
44+
if (err == 1) { ERR("Failed to Initialize!"); }
4445
if (err == 2) { return 0; }
4546
else if (filename == NULL) {
4647
printf("Input your programm (max. 1000 chars): ");
47-
program = (char*) malloc(sizeof(char) * 1024); ERR_ALLOC(program)
48+
program = (char*) malloc(sizeof(char) * 1024);
49+
if (program == NULL) { ERR_ALLOC(); }
50+
4851
fgets(program, 1024, stdin);
4952
program_len = strlen(program);
5053
} else {
@@ -54,16 +57,16 @@ int main(int argc, char const *argv[]) {
5457

5558
for (int i = 0; i < program_len; i++) {
5659
// Dynamicly alloc if the Memory gets too small
57-
if (mem_ptr >= memory_size) {
58-
unsigned char* new_memory = (unsigned char*) calloc(sizeof(unsigned char), (memory_size) + REALLOC_MEM); ERR_ALLOC(new_memory)
59-
memcpy(new_memory, memory, memory_size);
60-
memory_size += REALLOC_MEM;
61-
free(memory);
62-
memory = new_memory;
60+
if (mem_ptr >= memory_length) {
61+
int reerr = recallocMem((void**)&memory, memory_length,
62+
sizeof(unsigned char), (memory_length + REALLOC_MEM));
63+
if (reerr != 0) { ERR_ALLOC(); }
64+
65+
memory_length += REALLOC_MEM;
6366
}
6467

65-
current_instr = program[i];
66-
switch(current_instr) {
68+
// Process the current instruction
69+
switch(program[i]) {
6770
case '+':
6871
memory[mem_ptr] += 1;
6972
break;
@@ -122,16 +125,18 @@ int main(int argc, char const *argv[]) {
122125

123126
int loadProgr(const char* _file_name, char** _prog, size_t* _len) {
124127
FILE* _fp = fopen(_file_name, "rb");
125-
ERR_FOPEN(_fp, _file_name)
128+
if (_fp == NULL) { ERR_FOPEN(_file_name); return 1; }
126129

127130
fseek(_fp, 0, SEEK_END);
128131
long _flen = ftell(_fp);
129132
rewind(_fp);
130133
*_len = _flen + 1;
131134

132-
*_prog = (char*) malloc(sizeof(char) * (_flen + 1)); ERR_ALLOC(*_prog)
135+
*_prog = (char*) malloc(sizeof(char) * (_flen + 1));
136+
if (*_prog == NULL) { ERR_ALLOC(); return 1; }
133137

134138
fread(*_prog, sizeof(char), _flen, _fp);
139+
// Set 0 Terminator
135140
(*_prog)[_flen] = '\0';
136141

137142
fclose(_fp);

utils.c

+21-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "utils.h"
55

66
#ifndef YACBFI_VERSION
7-
#define YACBFI_VERSION "1.0.2"
7+
#define YACBFI_VERSION "1.0.5"
88
#endif
99

1010
int getargs(int _argc, char const *_argv[], Command _cmds[], int* _index) {
@@ -45,7 +45,7 @@ int getargs(int _argc, char const *_argv[], Command _cmds[], int* _index) {
4545

4646
void printhelp(Command _cmds[]) {
4747
// Print the Usage (Usage: Name [help])
48-
printf("Usage: %s %s\n", _cmds[0].name, _cmds[0].help);
48+
printf("\nUsage: %s %s\n", _cmds[0].name, _cmds[0].help);
4949

5050
// Print all helps ( -a, --name {spaces} help)
5151
for (int i = 1; _cmds[i].name != NULL; i++) {
@@ -82,8 +82,8 @@ int handleArgs(int _argc, char const *_argv[], Command _cmds[], const char** _fi
8282
_index++;
8383
break;
8484
case 'v':
85-
printf("\nYACBFI - Made by RedH - %s\n", YACBFI_VERSION);
86-
printf("This software is published under the MIT license.\nSee the LICENSE file for more information.\n\n");
85+
printf("\nYACBFI - Made by RedH - %s\n\n", YACBFI_VERSION);
86+
printf("This software is published under the MIT license.\nSee the LICENSE file for more information.\n");
8787
return 2;
8888
break;
8989
case '?':
@@ -93,5 +93,22 @@ int handleArgs(int _argc, char const *_argv[], Command _cmds[], const char** _fi
9393
break;
9494
}
9595
}
96+
return 0;
97+
}
98+
99+
int recallocMem(void** _mem, size_t _oldNitems, size_t _size, size_t _newNitems) {
100+
if (_newNitems <= _oldNitems) {
101+
return 0;
102+
}
103+
104+
void* _new_memory = calloc(_newNitems, _size);
105+
if (_new_memory == NULL) {
106+
return 1;
107+
}
108+
109+
memcpy(_new_memory, *_mem, _oldNitems);
110+
free(*_mem);
111+
*_mem = _new_memory;
112+
96113
return 0;
97114
}

utils.h

+17-6
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
#include <stdio.h>
99
#include <string.h>
1010

11-
#define ERR(msg) fprintf(stderr, msg); return 1;
12-
#define ERR_ALLOC(ptr) if (ptr == NULL) { fprintf(stderr, "Error allocating memory\n"); return 1; }
13-
#define ERR_FOPEN(fptr, fn) if (fptr == NULL) { fprintf(stderr, "Could not open the file \"%s\"\n", fn); return 1; }
11+
#define ERR(msg) fprintf(stderr, "\n\n%s\n\n", msg)
12+
#define ERR_ALLOC() fprintf(stderr, "\n\nError reallocating new Memory: %s\n\n", strerror(errno))
13+
#define ERR_FOPEN(fn) fprintf(stderr, "\n\nCould not open the file \"%s\": %s\n\n", fn, strerror(errno))
1414

1515
#define COMMAND_HAS_ARG 1
1616
#define COMMAND_NO_ARG 0
@@ -25,9 +25,9 @@ typedef struct {
2525

2626

2727
/**
28-
* @param _cmds A list of all possible args. The last has to be NULL (ptr) and 0 (char) termiantet
29-
* @param _filename Return value
30-
* @return 0 = Success, 1 = Err, 2 = End programm no Err
28+
* \param _cmds A list of all possible args. First has to be Program Name. The last has to be NULL (ptr) and 0 (char) termiantet
29+
* \param _filename Return value
30+
* \return 0 = Success, 1 = Err, 2 = End programm no Err
3131
*/
3232
int handleArgs(int _argc, char const *_argv[], Command _cmds[], const char** _filename);
3333

@@ -45,4 +45,15 @@ int getargs(int _argc, char const *_argv[], Command _cmds[], int* _index);
4545
*/
4646
void printhelp(Command _cmds[]);
4747

48+
/**
49+
* \brief Reallocate a block of memory to make it bigger (smaller not supportet). New space Iitialiced to 0 (using calloc)
50+
*
51+
* \param _mem Memory that needs to be reallocatet
52+
* \param _oldNitems The length of the old "array"
53+
* \param _size Size of the type that u allocate (sizeof())
54+
* \param _newNitems The length of the new "array"
55+
* \return 0 = Success, 1 = Err
56+
*/
57+
int recallocMem(void** _mem, size_t _oldNitems, size_t _size, size_t _newNitems);
58+
4859
#endif

0 commit comments

Comments
 (0)