Mercurial > sos > sos
view src/std.c @ 21:7c2adb65ceac
Started working on dynamic memory
I have a slightly adapted version of dynamic memory allocation from the H&R
book in the program now, I will soon be adding the free function.
I fixed sPrint so it will work with large buffers.
author | Jonathan Pevarnek <pevarnj@gmail.com> |
---|---|
date | Tue, 15 Mar 2011 22:55:36 -0400 |
parents | 525eab23e68a |
children | 3fb0ec050ff3 |
line wrap: on
line source
#include <std.h> char* itoa(int n, char *a) { char *ret = a; if(n < 0) { *a++ = '-'; n *= -1; } char *b = a; if(!n) *b++ = '0'; for(; n; b++) { *b = n%10 + '0'; n = n/10; } *b-- = '\0'; for(; a < b; a++, b--) { //reverse char temp = *b; *b = *a; *a = temp; } return ret; } char* ftoa(double x, char *a, unsigned int prec) { char *ret = a; int n = (int) x; //integer part double d = x - (double) n; //fractional part; itoa(n, a); if(prec) { //only do the decimal part if decimal parts were asked for while(*a && *++a); //get to the null character from itoa int i; //counter variable for the for loop *a++ = '.'; //put the decimal in place for(i = 0; i < prec; i++) { d = d*10; //the integer part is the decimal digit *a++ = ((int) d) + '0'; //add the integer part of d to the string d -= (int) d; //chop off the integer part } a--; //move back to the last digit while(*a != '.') { if(*a == '0') { a--; continue; } else if(*a > '0' && *a <= '9') { a++; break; } } *a = '\0'; } return ret; } int atoi(char *a) { short neg = 0; int n = 0; if(*a == '-') { neg = 1; a++; } else if(*a == '+') a++; while(*a >= '0' && *a <= '9') n = n*10 + (*a++ - '0'); if(neg) n *= -1; return n; } double atof(char *a) { int n = atoi(a); double x = 0; double dec = .1; while(*a != '.') { if(!(*a >= '0' && *a <= '9')) return n; a++; } a++; //a will be immediately after the decimal point while(*a >= '0' && *a <= '9') { //goes through the decimal part x += (*a - '0')*dec; dec *= .1; a++; } return n + x; } void sPrint(char *a) { char *b = a; while(*b && *++b); do { putline(a, (b - a > CON_LEN)?CON_LEN:(b - a)); a += CON_LEN; } while(a < b); } int strcmp(const char *a, const char *b) { while(1) { if(*a - *b) return *a - *b; if(*a == '\0') return 0; a++; b++; } return -1; } char* sGet(char *a, unsigned int n) { int length = getline(a, n); a[(length < n)?length:n - 1] = '\0'; return a; } int arrayLookup(char *text, const char array[][10], int last) { int i; for(i = 0; i < last; i++) if(!strcmp(array[i], text)) return i; return last; } char* append(char *dest, char *src) { char *ret = dest; while(*dest&& *++dest); //get to null in first string while((*dest++ = *src++)); return ret; } static Block *allocp = NULL; //the location of the last item allocated by malloc void* malloc(size_t size) { unsigned int nBlocks = 1 + (size + sizeof(Block) - 1)/sizeof(Block); //We are allocating a number of blocks equal to the number needed rounded up //plus one for the actual info block Block *cur, *prev; if(allocp == NULL) { allocp = (void*)0x200000; //this is where memory should start allocp->s.next = allocp; allocp->s.size = (0x1400000/sizeof(Block)); //I am going to limit the heap to 20MB for now } prev = allocp; for(cur = prev->s.next;; ) { if(cur->s.size >= nBlocks) { //this section is pretty much exactly copied from K&R if(cur->s.size == nBlocks) { prev->s.next = cur->s.next; } else { cur->s.size -= nBlocks; cur += cur->s.size; cur->s.size = nBlocks; } allocp = prev; return (void*)(cur + 1); } else if(cur == allocp) { //We went back to the start... return NULL; } } } /* void free(void *ptr) { } */