Skip to content

Commit a812978

Browse files
committed
Fix vector resize in copy constructor
1 parent 7c89013 commit a812978

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

src/usr/include/vector.tcc

+15-6
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,29 @@ static std::size_t next_power2(std::size_t size) {
1717
}
1818

1919
template <typename T>
20-
vector<T>::vector() : _data(NULL), _size(0) {
20+
vector<T>::vector() :
21+
_data(NULL),
22+
_capacity(0),
23+
_size(0) {
2124
resize_capacity(1);
2225
}
2326

2427
template <typename T>
25-
vector<T>::vector(std::size_t size, const T &_default): _size(size) {
26-
this->_data = NULL;
28+
vector<T>::vector(std::size_t size, const T &_default):
29+
_data(NULL),
30+
_capacity(0),
31+
_size(size) {
2732
resize_capacity(size);
2833
for(std::size_t i = 0; i < size; i++) {
2934
this->_data[i] = _default;
3035
}
3136
}
3237

3338
template <typename T>
34-
vector<T>::vector(const vector<T> &o) : _size(o._size) {
39+
vector<T>::vector(const vector<T> &o) :
40+
_data(NULL),
41+
_capacity(0),
42+
_size(o._size) {
3543
resize_capacity(o._capacity);
3644
std::memcpy(_data, o._data, _capacity*sizeof(T));
3745
}
@@ -62,14 +70,15 @@ template <typename T>
6270
typename vector<T>::const_iterator vector<T>::end() const { return this->_data + this->_size; }
6371

6472
template <typename T>
65-
void vector<T>::resize_capacity(std::size_t capacity) {
73+
void vector<T>::resize_capacity(volatile std::size_t capacity) {
6674
// internal; assumes capacity to be power of 2
6775
// and will also make a copy of array and move _data pointer.
6876
// assumes size<=current_capacity and size<=new_capacity
69-
const std::size_t data_size = capacity*sizeof(T);
77+
const std::size_t data_size = std::min(capacity, this->_capacity)*sizeof(T);
7078
T *_new_data = new T[capacity];
7179
if (!_new_data) return; // malloc failed
7280
std::memcpy(_new_data, this->_data, data_size);
81+
7382
delete[] this->_data; // should be no-op if _data == NULL
7483
this->_data = _new_data;
7584
this->_capacity = capacity;

src/usr/lib/stdlib.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ int benchmark_get_heap_usage() {
156156
}
157157

158158
void* malloc(size_t size) {
159+
size = ((size + 3) >> 2 ) << 2;
160+
size += 4; // 4 bytes to store size
161+
159162
void* loc = _heap_start + heap_head_offset;
160163
void* max_loc = get_current_esp()-heap_stack_safety_gap-size;
161164
if(loc>max_loc) {
@@ -165,11 +168,15 @@ void* malloc(size_t size) {
165168
}
166169
heap_head_offset += size;
167170
benchmark_heap_inuse += size;
168-
return loc;
171+
172+
*((uint32_t*)loc) = size;
173+
return (loc+4);
169174
}
170175

171176
void free(void* ptr) {
172177
if(ptr == NULL) return;
173178
// current version of malloc is non-optimal and doesn't
174179
// do any free operation.
180+
size_t size = *((uint32_t*)(ptr-4));
181+
benchmark_heap_inuse -= size;
175182
}

src/usr/local/src/logo.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ void frame_wait() {
2020
while (counter--);
2121
}
2222

23+
bool graceful_exit = false;
24+
2325
std::vector<std::string> split(std::string &str) {
2426
std::vector<std::string> rv;
2527
std::size_t len = str.length();
@@ -192,7 +194,8 @@ class Display {
192194
return 0;
193195
}
194196
if (cmd == "exit") {
195-
std::exit(0);
197+
graceful_exit = true;
198+
return 0;
196199
}
197200
return 10;
198201
}
@@ -385,7 +388,7 @@ class Interpreter {
385388
Interpreter() : display(console.get_y_offset()) {
386389
}
387390
void execute() {
388-
while(1) {
391+
while(!graceful_exit) {
389392
console.update_status(display);
390393
display.draw();
391394
console.draw();
@@ -408,7 +411,7 @@ void cleanup_graphics() {
408411
std::graphics::closegraph();
409412
std::cout << "logo graphics closed" << std::endl;
410413
std::cout << "heap memory at exit " <<
411-
(std::benchmark_get_heap_usage()/1024) << "KB" << std::endl;
414+
(std::benchmark_get_heap_usage()) << " bytes" << std::endl;
412415
}
413416

414417
int show_usage() {
@@ -448,5 +451,6 @@ int main(int argc,char *argv[]) {
448451
// graphics started
449452
std::atexit(cleanup_graphics);
450453
start_logo();
454+
std::cout<<" all over \n";
451455
return 0;
452456
}

0 commit comments

Comments
 (0)