Improve memory allocation logic, fix malloc.h for macOS GNU GCC
This commit is contained in:
parent
055c5f9c51
commit
b9b910439a
|
@ -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ÿÿ
|
70
buffer.c
70
buffer.c
|
@ -270,35 +270,33 @@ Buffer* b_addc(Buffer* const pBD, char symbol) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Reset reallocation flag */
|
||||||
pBD->r_flag = UNSET_R_FLAG;
|
pBD->r_flag = UNSET_R_FLAG;
|
||||||
/* BEGIN BUFFER INCREASE */
|
|
||||||
|
/* BEGIN BUFFER INCREMENT */
|
||||||
if (pBD->addc_offset == pBD->capacity){
|
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;
|
return NULL;
|
||||||
}
|
}
|
||||||
else if (pBD->mode == ADD_OP_MODE) { /* Calculate new size for additive mode */
|
else if (pBD->mode == ADD_OP_MODE) { /* Calculate new size for additive mode */
|
||||||
new_cap = pBD->capacity + (unsigned char) pBD->inc_factor;
|
new_cap = pBD->capacity + pBD->inc_factor;
|
||||||
|
/* Make sure no short overflow happened */
|
||||||
/* 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.
|
|
||||||
*/
|
|
||||||
if (new_cap < MIN_CAPACITY){
|
if (new_cap < MIN_CAPACITY){
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else if (pBD->mode == MUL_OP_MODE) { /* Calculate new size in multiplicative mode */
|
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;
|
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;
|
avail_space = SHRT_MAX - pBD->capacity;
|
||||||
new_inc = (avail_space * (unsigned char) pBD->inc_factor / 100);
|
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;
|
new_cap = SHRT_MAX;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -307,11 +305,13 @@ Buffer* b_addc(Buffer* const pBD, char symbol) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Reallocate memory to character buffer */
|
/* 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);
|
tmp_addr = (char *)realloc(pBD->cb_head, sizeof(char) * new_cap);
|
||||||
|
|
||||||
if (tmp_addr == NULL){
|
if (tmp_addr == NULL){
|
||||||
return NULL; /* Abort everything if allocation fails */
|
return NULL; /* Abort everything if allocation fails */
|
||||||
}
|
}
|
||||||
|
|
||||||
pBD->cb_head = tmp_addr;
|
pBD->cb_head = tmp_addr;
|
||||||
pBD->capacity = new_cap;
|
pBD->capacity = new_cap;
|
||||||
if (old_addr == pBD->cb_head) { /* Compare the old and new addresses and set flag appropriately */
|
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"); }
|
if (b_isempty(pBD) == TRUE) { printf("The Buffer is empty.\n"); }
|
||||||
else {
|
else {
|
||||||
pBD->getc_offset = OFFSET_RESET;
|
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 {
|
do {
|
||||||
char_buf = b_getc(pBD);
|
char_buf = b_getc(pBD);
|
||||||
printf("%c", (char) char_buf);
|
printf("%c", (char) char_buf);
|
||||||
|
@ -397,36 +387,24 @@ int b_print(Buffer* const pBD) {
|
||||||
int b_load(FILE* const fi, 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 */
|
char char_buf; /* "Buffer" character to be loaded before adding to cb_head */
|
||||||
int char_count = 0; /* Character counter */
|
int char_count = 0; /* Character counter */
|
||||||
/*char eof_flag;*/
|
|
||||||
|
|
||||||
if (!fi || !pBD) { return LOAD_FAIL; }
|
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 (!b_addc(pBD, char_buf)) { return LOAD_FAIL; }
|
|
||||||
}*/
|
|
||||||
do {
|
do {
|
||||||
char_buf = (char) fgetc(fi);
|
char_buf = (char) fgetc(fi);
|
||||||
if (char_buf == EOF) { break; }
|
if (char_buf == (char) EOF) {
|
||||||
|
|
||||||
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) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (b_addc(pBD, char_buf) == NULL) {
|
if (!b_addc(pBD, char_buf)) {
|
||||||
return LOAD_FAIL;
|
return LOAD_FAIL;
|
||||||
}
|
}
|
||||||
}*/
|
|
||||||
|
char_count++;
|
||||||
|
} while (!feof(fi));
|
||||||
|
|
||||||
return char_count;
|
return char_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
4
buffer.h
4
buffer.h
|
@ -14,7 +14,7 @@
|
||||||
|
|
||||||
/* standard header files */
|
/* standard header files */
|
||||||
#include <stdio.h> /* standard input/output */
|
#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 */
|
#include <limits.h> /* implementation-defined data type ranges and limits */
|
||||||
|
|
||||||
/* constant definitions */
|
/* constant definitions */
|
||||||
|
@ -30,7 +30,7 @@
|
||||||
#define R_FAIL2 -2 /* fail return value */
|
#define R_FAIL2 -2 /* fail return value */
|
||||||
#define LOAD_FAIL -2 /* load fail error */
|
#define LOAD_FAIL -2 /* load fail error */
|
||||||
#define SET_R_FLAG 1 /* realloc flag set value */
|
#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 SET_EOB_FLAG 1 /* set end of buffer flag */
|
||||||
#define UNSET_EOB_FLAG 0 /* unset end of buffer flag */
|
#define UNSET_EOB_FLAG 0 /* unset end of buffer flag */
|
||||||
|
|
||||||
|
|
BIN
buffer.h.gch
BIN
buffer.h.gch
Binary file not shown.
|
@ -1,8 +1,8 @@
|
||||||
/* File name: platy_bt.c
|
/* File name: platy_bt.c
|
||||||
* Purpose:This is the main program for Assignment #1, CST8152, Fall 16
|
* Purpose:This is the main program for Assignment #1, CST8152, Winter 17
|
||||||
* Version: 1.16.2
|
* Version: 1.17.1
|
||||||
* Author: Svillen Ranev
|
* 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
|
/* The #define _CRT_SECURE_NO_WARNINGS should be used in MS Visual Studio projects
|
||||||
|
|
Binary file not shown.
|
@ -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ÿÿ
|
|
@ -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ÿÿ
|
|
@ -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ÿÿ
|
|
@ -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ÿÿ
|
|
@ -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ÿÿ
|
|
@ -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ÿÿ
|
Loading…
Reference in New Issue