Files
NJU_PA/abstract-machine/klib/src/stdlib.c
tracer-ics2023 2f30e78edd > compile NEMU
221220000 张三
Linux zzy 5.15.146.1-microsoft-standard-WSL2 #1 SMP Thu Jan 11 04:09:03 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
 20:43:23 up 2 days, 10:57,  1 user,  load average: 0.55, 0.55, 0.46
2024-09-14 20:43:23 +08:00

50 lines
1.1 KiB
C

#include <am.h>
#include <klib.h>
#include <klib-macros.h>
#if !defined(__ISA_NATIVE__) || defined(__NATIVE_USE_KLIB__)
static unsigned long int next = 1;
int rand(void) {
// RAND_MAX assumed to be 32767
next = next * 1103515245 + 12345;
return (unsigned int)(next/65536) % 32768;
}
void srand(unsigned int seed) {
next = seed;
}
int abs(int x) {
return (x < 0 ? -x : x);
}
int atoi(const char* nptr) {
int x = 0;
while (*nptr == ' ') { nptr ++; }
while (*nptr >= '0' && *nptr <= '9') {
x = x * 10 + *nptr - '0';
nptr ++;
}
return x;
}
static uint8_t* ptr;
void *malloc(size_t size) {
// On native, malloc() will be called during initializaion of C runtime.
// Therefore do not call panic() here, else it will yield a dead recursion:
// panic() -> putchar() -> (glibc) -> malloc() -> panic()
#if !(defined(__ISA_NATIVE__) && defined(__NATIVE_USE_KLIB__))
if (ptr == NULL) ptr = heap.start;
// assert(ptr + size <= heap.end && ptr >= heap.start);
ptr += size;
return ptr - size;
#endif
return NULL;
}
void free(void *ptr) {
}
#endif