os161-1.99
 All Data Structures
lib.h
00001 /*
00002  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
00003  *      The President and Fellows of Harvard College.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 #ifndef _LIB_H_
00031 #define _LIB_H_
00032 
00033 /*
00034  * Miscellaneous standard C functions for the kernel, and other widely used
00035  * kernel functions.
00036  *
00037  * Note: setjmp and longjmp are in <setjmp.h>.
00038  */
00039 
00040 
00041 #include <cdefs.h>
00042 
00043 /*
00044  * Assert macros.
00045  *
00046  * KASSERT and DEBUGASSERT are the same, except that they can be
00047  * toggled independently. DEBUGASSERT is used in places where making
00048  * checks is likely to be expensive and relatively unlikely to be
00049  * helpful.
00050  *
00051  * Note that there's also a COMPILE_ASSERT for compile-time checks;
00052  * it's in <cdefs.h>.
00053  *
00054  * Regular assertions (KASSERT) are disabled by the kernel config
00055  * option "noasserts". DEBUGASSERT could be controlled by kernel
00056  * config also, but since it's likely to be wanted only rarely during
00057  * testing there doesn't seem much point; one can just edit this file
00058  * temporarily instead.
00059  */
00060 #include "opt-noasserts.h"
00061 
00062 #if OPT_NOASSERTS
00063 #define KASSERT(expr) ((void)(expr))
00064 #else
00065 #define KASSERT(expr) \
00066         ((expr) ? (void)0 : badassert(#expr, __FILE__, __LINE__, __func__))
00067 #endif
00068 
00069 #if 1 /* no debug asserts */
00070 #define DEBUGASSERT(expr) ((void)(expr))
00071 #else
00072 #define DEBUGASSERT(expr) \
00073         ((expr) ? (void)0 : badassert(#expr, __FILE__, __LINE__, __func__))
00074 #endif
00075 
00076 /*
00077  * Bit flags for DEBUG()
00078  */
00079 #define DB_LOCORE      0x0001
00080 #define DB_SYSCALL     0x0002
00081 #define DB_INTERRUPT   0x0004
00082 #define DB_DEVICE      0x0008
00083 #define DB_THREADS     0x0010
00084 #define DB_VM          0x0020
00085 #define DB_EXEC        0x0040
00086 #define DB_VFS         0x0080
00087 #define DB_SFS         0x0100
00088 #define DB_NET         0x0200
00089 #define DB_NETFS       0x0400
00090 #define DB_KMALLOC     0x0800
00091 #define DB_SYNCPROB    0x1000
00092 
00093 extern uint32_t dbflags;
00094 
00095 /*
00096  * DEBUG() is for conditionally printing debug messages to the console.
00097  *
00098  * The idea is that you put lots of lines of the form
00099  *
00100  *      DEBUG(DB_VM, "VM free pages: %u\n", free_pages);
00101  *
00102  * throughout the kernel; then you can toggle whether these messages
00103  * are printed or not at runtime by setting the value of dbflags with
00104  * the debugger.
00105  *
00106  * Unfortunately, as of this writing, there are only a very few such
00107  * messages actually present in the system yet. Feel free to add more.
00108  *
00109  * DEBUG is a varargs macro. These were added to the language in C99.
00110  */
00111 #define DEBUG(d, ...) ((dbflags & (d)) ? kprintf(__VA_ARGS__) : 0)
00112 
00113 /*
00114  * Random number generator, using the random device.
00115  *
00116  * random() returns a number between 0 and randmax() inclusive.
00117  */
00118 #define RANDOM_MAX (randmax())
00119 uint32_t randmax(void);
00120 uint32_t random(void);
00121 
00122 /*
00123  * Kernel heap memory allocation. Like malloc/free.
00124  * If out of memory, kmalloc returns NULL.
00125  */
00126 void *kmalloc(size_t size);
00127 void kfree(void *ptr);
00128 void kheap_printstats(void);
00129 
00130 /*
00131  * C string functions. 
00132  *
00133  * kstrdup is like strdup, but calls kmalloc instead of malloc.
00134  * If out of memory, it returns NULL.
00135  */
00136 size_t strlen(const char *str);
00137 int strcmp(const char *str1, const char *str2);
00138 char *strcpy(char *dest, const char *src);
00139 char *strcat(char *dest, const char *src);
00140 char *kstrdup(const char *str);
00141 char *strchr(const char *searched, int searchfor);
00142 char *strrchr(const char *searched, int searchfor);
00143 char *strtok_r(char *buf, const char *seps, char **context);
00144 
00145 void *memcpy(void *dest, const void *src, size_t len);
00146 void *memmove(void *dest, const void *src, size_t len);
00147 void bzero(void *ptr, size_t len);
00148 int atoi(const char *str);
00149 
00150 int snprintf(char *buf, size_t maxlen, const char *fmt, ...) __PF(3,4);
00151 
00152 const char *strerror(int errcode);
00153 
00154 /*
00155  * Low-level console access.
00156  *
00157  * putch_prepare and putch_complete should be called around a series
00158  * of putch() calls, if printing in polling mode is a possibility.
00159  * kprintf does this.
00160  */
00161 void putch(int ch);
00162 void putch_prepare(void);
00163 void putch_complete(void);
00164 int getch(void);
00165 void beep(void);
00166 
00167 /*
00168  * Higher-level console output.
00169  *
00170  * kprintf is like printf, only in the kernel.
00171  * panic prepends the string "panic: " to the message printed, and then
00172  * resets the system.
00173  * badassert calls panic in a way suitable for an assertion failure.
00174  * kgets is like gets, only with a buffer size argument.
00175  *
00176  * kprintf_bootstrap sets up a lock for kprintf and should be called
00177  * during boot once malloc is available and before any additional
00178  * threads are created.
00179  */
00180 int kprintf(const char *format, ...) __PF(1,2);
00181 void panic(const char *format, ...) __PF(1,2);
00182 void badassert(const char *expr, const char *file, int line, const char *func);
00183 
00184 void kgets(char *buf, size_t maxbuflen);
00185 
00186 void kprintf_bootstrap(void);
00187 
00188 /*
00189  * Other miscellaneous stuff
00190  */
00191 
00192 #define DIVROUNDUP(a,b) (((a)+(b)-1)/(b))
00193 #define ROUNDUP(a,b)    (DIVROUNDUP(a,b)*b)
00194 
00195 
00196 #endif /* _LIB_H_ */
 All Data Structures