-
Notifications
You must be signed in to change notification settings - Fork 415
Remaining malloc to new #2100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remaining malloc to new #2100
Changes from 34 commits
7d2e180
e9ae316
6d13214
6797d8d
4ee9a81
bcc3bcf
1070e65
2c0373e
9c3b9f9
a78ec80
e3d56c3
e3a4a0d
bb63fb7
60e1553
83f1a3c
46cc019
754aedc
357640d
1707790
0480d2c
10b1c9c
282b1b5
52a6a78
4f6ac3a
65a82ba
59fe896
8bc977f
bb3540a
a966dd7
46c2321
0b1cbe8
1b2aefb
954e48d
3e605ed
2da2652
02bd2db
5cd437c
67ea56d
b4f35bd
6035137
263dfa2
5d7ea7f
d68436a
e5a1a9c
abf58ea
959bd4b
19bcf19
b1b82a7
aace716
ec774b6
88e16dc
4c4f815
e7380ff
321a85b
4397396
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ t_linked_vptr* insert_in_vptr_list(t_linked_vptr* head, void* vptr_to_add) { | |
|
||
t_linked_vptr* linked_vptr; | ||
|
||
linked_vptr = (t_linked_vptr*)vtr::malloc(sizeof(t_linked_vptr)); | ||
linked_vptr = new t_linked_vptr; | ||
|
||
linked_vptr->data_vptr = vptr_to_add; | ||
linked_vptr->next = head; | ||
|
@@ -26,7 +26,7 @@ t_linked_vptr* delete_in_vptr_list(t_linked_vptr* head) { | |
if (head == nullptr) | ||
return nullptr; | ||
linked_vptr = head->next; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For readability, I'd move the declaration from line 24 to here.
(Following the rule to declare variables as close to the point where we need it, ideally using an opportunity to |
||
free(head); | ||
delete head; | ||
return linked_vptr; /* New head of the list */ | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#include <cstddef> | ||
#include <cstdlib> | ||
#include <math.h> | ||
|
||
#include "vtr_assert.h" | ||
#include "vtr_list.h" | ||
|
@@ -101,7 +102,9 @@ void* chunk_malloc(size_t size, t_chunk* chunk_info) { | |
|
||
if ((size_t)(chunk_info->mem_avail) < size) { /* Need to malloc more memory. */ | ||
if (size > CHUNK_SIZE) { /* Too big, use standard routine. */ | ||
tmp_ptr = (char*)vtr::malloc(size); | ||
/* Want to allocate a block of memory the size of size. | ||
* i.e. malloc(size) */ | ||
tmp_ptr = new char[(int)ceil(size / sizeof(char))]; | ||
jmah76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/* When debugging, uncomment the code below to see if memory allocation size */ | ||
/* makes sense */ | ||
|
@@ -116,7 +119,7 @@ void* chunk_malloc(size_t size, t_chunk* chunk_info) { | |
} | ||
|
||
if (chunk_info->mem_avail < FRAGMENT_THRESHOLD) { /* Only a small scrap left. */ | ||
chunk_info->next_mem_loc_ptr = (char*)vtr::malloc(CHUNK_SIZE); | ||
chunk_info->next_mem_loc_ptr = new char[(int)ceil(CHUNK_SIZE / sizeof(char))]; | ||
chunk_info->mem_avail = CHUNK_SIZE; | ||
VTR_ASSERT(chunk_info != nullptr); | ||
chunk_info->chunk_ptr_head = insert_in_vptr_list(chunk_info->chunk_ptr_head, chunk_info->next_mem_loc_ptr); | ||
|
@@ -127,9 +130,10 @@ void* chunk_malloc(size_t size, t_chunk* chunk_info) { | |
* to allocate normally. */ | ||
|
||
else { | ||
tmp_ptr = (char*)vtr::malloc(size); | ||
tmp_ptr = new char[(int)ceil(size / sizeof(char))]; | ||
VTR_ASSERT(chunk_info != nullptr); | ||
chunk_info->chunk_ptr_head = insert_in_vptr_list(chunk_info->chunk_ptr_head, tmp_ptr); | ||
|
||
return (tmp_ptr); | ||
jmah76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
@@ -157,11 +161,14 @@ void free_chunk_memory(t_chunk* chunk_info) { | |
curr_ptr = chunk_info->chunk_ptr_head; | ||
|
||
while (curr_ptr != nullptr) { | ||
free(curr_ptr->data_vptr); /* Free memory "chunk". */ | ||
/* Must cast pointers to type char*, since the're of type void*, which delete can't | ||
* be called on.*/ | ||
delete[]((char*)curr_ptr->data_vptr); /* Free memory "chunk". */ | ||
prev_ptr = curr_ptr; | ||
curr_ptr = curr_ptr->next; | ||
free(prev_ptr); /* Free memory used to track "chunk". */ | ||
delete (t_linked_vptr*)prev_ptr; /* Free memory used to track "chunk". */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the typecast ? |
||
} | ||
|
||
chunk_info->chunk_ptr_head = nullptr; | ||
chunk_info->mem_avail = 0; | ||
chunk_info->next_mem_loc_ptr = nullptr; | ||
|
Uh oh!
There was an error while loading. Please reload this page.