Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use heap allocation for varlength buffers
  • Loading branch information
djphoenix committed Nov 18, 2016
1 parent 0c63eb9 commit fbc7edb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/kernlib/printf.cpp
Expand Up @@ -15,6 +15,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include "kernlib.hpp"
#include "heap.hpp"

static inline char *longlong_to_string(
char *buf, size_t len, uint64_t n, uint8_t base,
Expand Down Expand Up @@ -359,10 +360,16 @@ int vprintf(const char *format, va_list ap) {
va_copy(tmp, ap);
int len = vsnprintf(0, 0, format, tmp);
va_end(tmp);
char *buf = static_cast<char*>(alloca(len + 1));
char smbuf[512], *buf;
if (len > 511) {
buf = static_cast<char*>(Heap::alloc(len + 1));
} else {
buf = smbuf;
}
len = vsnprintf(buf, len, format, ap);
buf[len] = 0;
Display::getInstance()->write(buf);
if (len > 511) Heap::free(buf);
return len;
}

Expand Down
7 changes: 5 additions & 2 deletions src/platform/cpu.cpp
Expand Up @@ -16,6 +16,7 @@

#include "cpu.hpp"
#include "kernlib.hpp"
#include "heap.hpp"

char CPU::vendor[13] = "";
char CPU::brandString[49] = "";
Expand Down Expand Up @@ -180,7 +181,7 @@ char* CPU::getFeaturesStr() {
uint32_t count = bitcnt(f) + bitcnt(ef) + bitcnt(fe);

size_t bufsize = 17 * count;
char *buf = static_cast<char*>(alloca(bufsize));
char *buf = static_cast<char*>(Heap::alloc(bufsize));
char *end = buf;

for (int i = 0; i < 64; i++) {
Expand All @@ -205,7 +206,9 @@ char* CPU::getFeaturesStr() {
end--;
end[0] = 0;

return strdup(buf);
buf = Heap::realloc(buf, strlen(buf) + 1);

return buf;
}

char* CPU::getBrandString() {
Expand Down

0 comments on commit fbc7edb

Please sign in to comment.