You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stdlib.h 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #ifndef STDLIB_H
  2. #define STDLIB_H
  3. FILE_LICENCE ( GPL2_OR_LATER );
  4. #include <stdint.h>
  5. #include <assert.h>
  6. /*****************************************************************************
  7. *
  8. * Numeric parsing
  9. *
  10. ****************************************************************************
  11. */
  12. static inline int strtoul_base ( const char **pp, int base )
  13. {
  14. const char *p = *pp;
  15. if ( base == 0 ) {
  16. base = 10;
  17. if ( *p == '0' ) {
  18. p++;
  19. base = 8;
  20. if ( ( *p | 0x20 ) == 'x' ) {
  21. p++;
  22. base = 16;
  23. }
  24. }
  25. }
  26. *pp = p;
  27. return base;
  28. }
  29. static inline unsigned int strtoul_charval ( unsigned int charval )
  30. {
  31. if ( charval >= 'a' ) {
  32. charval = ( charval - 'a' + 10 );
  33. } else if ( charval >= 'A' ) {
  34. charval = ( charval - 'A' + 10 );
  35. } else if ( charval <= '9' ) {
  36. charval = ( charval - '0' );
  37. }
  38. return charval;
  39. }
  40. extern unsigned long strtoul ( const char *p, char **endp, int base );
  41. extern unsigned long long strtoull ( const char *p, char **endp, int base );
  42. /*****************************************************************************
  43. *
  44. * Memory allocation
  45. *
  46. ****************************************************************************
  47. */
  48. extern void * __malloc malloc ( size_t size );
  49. extern void * realloc ( void *old_ptr, size_t new_size );
  50. extern void free ( void *ptr );
  51. extern void * __malloc zalloc ( size_t len );
  52. /**
  53. * Allocate cleared memory
  54. *
  55. * @v nmemb Number of members
  56. * @v size Size of each member
  57. * @ret ptr Allocated memory
  58. *
  59. * Allocate memory as per malloc(), and zero it.
  60. *
  61. * This is implemented as a static inline, with the body of the
  62. * function in zalloc(), since in most cases @c nmemb will be 1 and
  63. * doing the multiply is just wasteful.
  64. */
  65. static inline void * __malloc calloc ( size_t nmemb, size_t size ) {
  66. return zalloc ( nmemb * size );
  67. }
  68. /*****************************************************************************
  69. *
  70. * Random number generation
  71. *
  72. ****************************************************************************
  73. */
  74. extern long int random ( void );
  75. extern void srandom ( unsigned int seed );
  76. static inline int rand ( void ) {
  77. return random();
  78. }
  79. static inline void srand ( unsigned int seed ) {
  80. srandom ( seed );
  81. }
  82. /*****************************************************************************
  83. *
  84. * Miscellaneous
  85. *
  86. ****************************************************************************
  87. */
  88. extern int system ( const char *command );
  89. extern __asmcall int main ( void );
  90. #endif /* STDLIB_H */