Improve memory allocation logic, fix malloc.h for macOS GNU GCC

This commit is contained in:
Victor Fernandes 2017-01-30 16:20:43 -05:00
parent 055c5f9c51
commit b9b910439a
15 changed files with 276 additions and 51 deletions

0
ass1e.pls Executable file
View File

33
ass1fi_test.out Normal file
View File

@ -0,0 +1,33 @@
Reading file ass1.pls ....Please wait
The input file ass1.pls is not completely loaded.
Input file size: 327
Printing buffer parameters:
The capacity of the buffer is: 200
The current size of the buffer is: 200
The operational mode of the buffer is: 0
The increment factor of the buffer is: 0
The current mark of the buffer is: 200
Printing buffer contents:
Compilers are fundamental to modern computing.
They act as translators, transforming human-oriented
language into computer-oriented machine-language.
A compiler allows virtually all coÿ
Printing buffer parameters:
The capacity of the buffer is: 201
The current size of the buffer is: 201
The operational mode of the buffer is: 0
The increment factor of the buffer is: 0
The current mark of the buffer is: 200
Printing buffer contents:
Compilers are fundamental to modern computing.
They act as translators, transforming human-oriented
language into computer-oriented machine-language.
A compiler allows virtually all coÿÿ

View File

@ -270,35 +270,33 @@ Buffer* b_addc(Buffer* const pBD, char symbol) {
return NULL;
}
/* Reset reallocation flag */
pBD->r_flag = UNSET_R_FLAG;
/* BEGIN BUFFER INCREASE */
/* BEGIN BUFFER INCREMENT */
if (pBD->addc_offset == pBD->capacity){
if (pBD->mode == FIX_OP_MODE) { /* Fixed mode */
if (pBD->mode == FIX_OP_MODE) { /* Fixed mode, won't increment */
return NULL;
}
else if (pBD->mode == ADD_OP_MODE) { /* Calculate new size for additive mode */
new_cap = pBD->capacity + (unsigned char) pBD->inc_factor;
/* Maximum additive increment is 255, therefore, if the current
capacity happens to be SHRT_MAX - 1, the value will underflow to a negative number
every time, and never "wrap" to a value greater than 1.
*/
new_cap = pBD->capacity + pBD->inc_factor;
/* Make sure no short overflow happened */
if (new_cap < MIN_CAPACITY){
return NULL;
}
}
else if (pBD->mode == MUL_OP_MODE) { /* Calculate new size in multiplicative mode */
if (pBD->capacity == SHRT_MAX){
if (pBD->capacity == SHRT_MAX){ /* Do nothing if at maximum size */
return NULL;
}
/* Mathematically, new_inc cannot be negative, since avail_space will
always be greater or equal to 0.
*/
avail_space = SHRT_MAX - pBD->capacity;
new_inc = (avail_space * (unsigned char) pBD->inc_factor / 100);
/* The same situation in Additive mode (L284) applies to Multiplicative Mode */
if (new_inc <= MIN_CAPACITY && pBD->capacity < SHRT_MAX) {
/* Check if there is enough space for the new increment and
"trim" it if needed */
if (new_inc >= avail_space) {
new_cap = SHRT_MAX;
}
else {
@ -307,11 +305,13 @@ Buffer* b_addc(Buffer* const pBD, char symbol) {
}
/* Reallocate memory to character buffer */
old_addr = pBD->cb_head; /* Keep track of old pointer address for checking if it changed */
old_addr = pBD->cb_head; /* Keep track of old pointer address to check if it changed */
tmp_addr = (char *)realloc(pBD->cb_head, sizeof(char) * new_cap);
if (tmp_addr == NULL){
return NULL; /* Abort everything if allocation fails */
}
pBD->cb_head = tmp_addr;
pBD->capacity = new_cap;
if (old_addr == pBD->cb_head) { /* Compare the old and new addresses and set flag appropriately */
@ -363,16 +363,6 @@ int b_print(Buffer* const pBD) {
if (b_isempty(pBD) == TRUE) { printf("The Buffer is empty.\n"); }
else {
pBD->getc_offset = OFFSET_RESET;
/* Trick the buffer into resetting the eob while printing the contents.
* The EOB flag will be set again once the loop is finished.
*
char_buf = b_getc(pBD);
b_retract(pBD);
for (char_count = 0; b_eob(pBD) == FALSE; char_count++) {
char_buf = b_getc(pBD);
printf("%c", (char) char_buf);
}
printf("\n");*/
do {
char_buf = b_getc(pBD);
printf("%c", (char) char_buf);
@ -397,36 +387,24 @@ int b_print(Buffer* const pBD) {
int b_load(FILE* const fi, Buffer* const pBD) {
char char_buf; /* "Buffer" character to be loaded before adding to cb_head */
int char_count = 0; /* Character counter */
/*char eof_flag;*/
if (!fi || !pBD) { return LOAD_FAIL; }
/*
for (char_count = 0; !feof(fi); char_count++) {
char_buf = (char) fgetc(fi);
if (char_buf == EOF) { break; }
if (!fi || !pBD) {
return LOAD_FAIL;
}
if (!b_addc(pBD, char_buf)) { return LOAD_FAIL; }
}*/
do {
char_buf = (char) fgetc(fi);
if (char_buf == EOF) { break; }
if (!b_addc(pBD, char_buf)) { return LOAD_FAIL; }
char_count++;
} while (!feof(fi));
/*eof_flag = feof(fi);
for (char_count = 0; !eof_flag; char_count++) {
char_buf = (char) fgetc(fi);
eof_flag = feof(fi);
if (char_buf == EOF) {
if (char_buf == (char) EOF) {
break;
}
if (b_addc(pBD, char_buf) == NULL) {
if (!b_addc(pBD, char_buf)) {
return LOAD_FAIL;
}
}*/
char_count++;
} while (!feof(fi));
return char_count;
}

View File

@ -14,7 +14,7 @@
/* standard header files */
#include <stdio.h> /* standard input/output */
#include <malloc.h> /* for dynamic memory allocation*/
#include <mm_malloc.h> /* for dynamic memory allocation*/
#include <limits.h> /* implementation-defined data type ranges and limits */
/* constant definitions */
@ -30,7 +30,7 @@
#define R_FAIL2 -2 /* fail return value */
#define LOAD_FAIL -2 /* load fail error */
#define SET_R_FLAG 1 /* realloc flag set value */
#define UNSET_R_FLAG 0 /* unset realloc flag */
#define UNSET_R_FLAG 0 /* unset reallocation flag */
#define SET_EOB_FLAG 1 /* set end of buffer flag */
#define UNSET_EOB_FLAG 0 /* unset end of buffer flag */

Binary file not shown.

BIN
platy

Binary file not shown.

View File

@ -1,8 +1,8 @@
/* File name: platy_bt.c
* Purpose:This is the main program for Assignment #1, CST8152, Fall 16
* Version: 1.16.2
* Purpose:This is the main program for Assignment #1, CST8152, Winter 17
* Version: 1.17.1
* Author: Svillen Ranev
* Date: 6 September 2016
* Date: 9 January 2017
*/
/* The #define _CRT_SECURE_NO_WARNINGS should be used in MS Visual Studio projects

BIN
platy_gnu Executable file

Binary file not shown.

Binary file not shown.

33
test.out Normal file
View File

@ -0,0 +1,33 @@
Reading file ass1.pls ....Please wait
The input file ass1.pls is not completely loaded.
Input file size: 327
Printing buffer parameters:
The capacity of the buffer is: 200
The current size of the buffer is: 200
The operational mode of the buffer is: 0
The increment factor of the buffer is: 0
The current mark of the buffer is: 200
Printing buffer contents:
Compilers are fundamental to modern computing.
They act as translators, transforming human-oriented
language into computer-oriented machine-language.
A compiler allows virtually all coÿ
Printing buffer parameters:
The capacity of the buffer is: 201
The current size of the buffer is: 201
The operational mode of the buffer is: 0
The increment factor of the buffer is: 0
The current mark of the buffer is: 200
Printing buffer contents:
Compilers are fundamental to modern computing.
They act as translators, transforming human-oriented
language into computer-oriented machine-language.
A compiler allows virtually all coÿÿ

41
test/ass1ai_test.out Normal file
View File

@ -0,0 +1,41 @@
Reading file ass1.pls ....Please wait
Printing buffer parameters:
The capacity of the buffer is: 335
The current size of the buffer is: 327
The operational mode of the buffer is: 1
The increment factor of the buffer is: 15
The current mark of the buffer is: 327
Printing buffer contents:
Compilers are fundamental to modern computing.
They act as translators, transforming human-oriented
language into computer-oriented machine-language.
A compiler allows virtually all computer users to
ignore the machine-dependent details of machine language.
Isn't that nice? =:)
CST8152
Autumn, 2016ÿ
Printing buffer parameters:
The capacity of the buffer is: 328
The current size of the buffer is: 328
The operational mode of the buffer is: 1
The increment factor of the buffer is: 15
The current mark of the buffer is: 327
Printing buffer contents:
Compilers are fundamental to modern computing.
They act as translators, transforming human-oriented
language into computer-oriented machine-language.
A compiler allows virtually all computer users to
ignore the machine-dependent details of machine language.
Isn't that nice? =:)
CST8152
Autumn, 2016ÿÿ

33
test/ass1fi_test.out Normal file
View File

@ -0,0 +1,33 @@
Reading file ass1.pls ....Please wait
The input file ass1.pls is not completely loaded.
Input file size: 327
Printing buffer parameters:
The capacity of the buffer is: 200
The current size of the buffer is: 200
The operational mode of the buffer is: 0
The increment factor of the buffer is: 0
The current mark of the buffer is: 200
Printing buffer contents:
Compilers are fundamental to modern computing.
They act as translators, transforming human-oriented
language into computer-oriented machine-language.
A compiler allows virtually all coÿ
Printing buffer parameters:
The capacity of the buffer is: 201
The current size of the buffer is: 201
The operational mode of the buffer is: 0
The increment factor of the buffer is: 0
The current mark of the buffer is: 200
Printing buffer contents:
Compilers are fundamental to modern computing.
They act as translators, transforming human-oriented
language into computer-oriented machine-language.
A compiler allows virtually all coÿÿ

41
test/ass1mi_test.out Normal file
View File

@ -0,0 +1,41 @@
Reading file ass1.pls ....Please wait
Printing buffer parameters:
The capacity of the buffer is: 5085
The current size of the buffer is: 327
The operational mode of the buffer is: -1
The increment factor of the buffer is: 15
The current mark of the buffer is: 327
Printing buffer contents:
Compilers are fundamental to modern computing.
They act as translators, transforming human-oriented
language into computer-oriented machine-language.
A compiler allows virtually all computer users to
ignore the machine-dependent details of machine language.
Isn't that nice? =:)
CST8152
Autumn, 2016ÿ
Printing buffer parameters:
The capacity of the buffer is: 328
The current size of the buffer is: 328
The operational mode of the buffer is: -1
The increment factor of the buffer is: 15
The current mark of the buffer is: 327
Printing buffer contents:
Compilers are fundamental to modern computing.
They act as translators, transforming human-oriented
language into computer-oriented machine-language.
A compiler allows virtually all computer users to
ignore the machine-dependent details of machine language.
Isn't that nice? =:)
CST8152
Autumn, 2016ÿÿ

33
test_gnu.out Normal file
View File

@ -0,0 +1,33 @@
Reading file ass1.pls ....Please wait
The input file ass1.pls is not completely loaded.
Input file size: 327
Printing buffer parameters:
The capacity of the buffer is: 200
The current size of the buffer is: 200
The operational mode of the buffer is: 0
The increment factor of the buffer is: 0
The current mark of the buffer is: 200
Printing buffer contents:
Compilers are fundamental to modern computing.
They act as translators, transforming human-oriented
language into computer-oriented machine-language.
A compiler allows virtually all coÿ
Printing buffer parameters:
The capacity of the buffer is: 201
The current size of the buffer is: 201
The operational mode of the buffer is: 0
The increment factor of the buffer is: 0
The current mark of the buffer is: 200
Printing buffer contents:
Compilers are fundamental to modern computing.
They act as translators, transforming human-oriented
language into computer-oriented machine-language.
A compiler allows virtually all coÿÿ

33
test_llvm.out Normal file
View File

@ -0,0 +1,33 @@
Reading file ass1.pls ....Please wait
The input file ass1.pls is not completely loaded.
Input file size: 327
Printing buffer parameters:
The capacity of the buffer is: 200
The current size of the buffer is: 200
The operational mode of the buffer is: 0
The increment factor of the buffer is: 0
The current mark of the buffer is: 200
Printing buffer contents:
Compilers are fundamental to modern computing.
They act as translators, transforming human-oriented
language into computer-oriented machine-language.
A compiler allows virtually all coÿ
Printing buffer parameters:
The capacity of the buffer is: 201
The current size of the buffer is: 201
The operational mode of the buffer is: 0
The increment factor of the buffer is: 0
The current mark of the buffer is: 200
Printing buffer contents:
Compilers are fundamental to modern computing.
They act as translators, transforming human-oriented
language into computer-oriented machine-language.
A compiler allows virtually all coÿÿ