Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

alloc.h

Go to the documentation of this file.
00001 /* -*- linux-c -*- */
00002 /** @file alloc.h
00003  * @brief Memory allocation functions.
00004  * @todo Should really be alloc.c for consistency.
00005  */
00006 
00007 enum errorcode { ERR_NONE=0, ERR_NO_MEM };
00008 enum errorcode _stp_error = ERR_NONE;
00009 
00010 /** Allocates memory within a probe.
00011  * This is used for small allocations from within a running
00012  * probe where the process cannot sleep. 
00013  * @param len Number of bytes to allocate.
00014  * @return a valid pointer on success or NULL on failure.
00015  * @bug Currently uses kmalloc (GFP_ATOMIC).
00016  */
00017 
00018 void *_stp_alloc(size_t len)
00019 {
00020         void *ptr = kmalloc(len, GFP_ATOMIC);
00021         if (unlikely(ptr == NULL))
00022                 _stp_error = ERR_NO_MEM;
00023         return ptr;
00024 }
00025 
00026 /** Allocates and clears memory within a probe.
00027  * This is used for small allocations from within a running
00028  * probe where the process cannot sleep. 
00029  * @param len Number of bytes to allocate.
00030  * @return a valid pointer on success or NULL on failure.
00031  * @bug Currently uses kmalloc (GFP_ATOMIC).
00032  */
00033 
00034 void *_stp_calloc(size_t len)
00035 {
00036         void *ptr = _stp_alloc(len);
00037         if (likely(ptr))
00038                 memset(ptr, 0, len);
00039         return ptr;
00040 }
00041 
00042 /** Allocates and clears memory outside a probe.
00043  * This is typically used in the module initialization to
00044  * allocate new maps, lists, etc.
00045  * @param len Number of bytes to allocate.
00046  * @return a valid pointer on success or NULL on failure.
00047  */
00048 
00049 void *_stp_valloc(size_t len)
00050 {
00051         void *ptr = vmalloc(len);
00052         if (likely(ptr))
00053                 memset(ptr, 0, len);
00054         else
00055                 _stp_error = ERR_NO_MEM;
00056         return ptr;
00057 }
00058 
00059 /** Frees memory allocated by _stp_alloc or _stp_calloc.
00060  * @param ptr pointer to memory to free
00061  */
00062 
00063 void _stp_free(void *ptr)
00064 {
00065         if (likely(ptr))
00066                 kfree(ptr);
00067 }
00068 
00069 /** Frees memory allocated by _stp_valloc.
00070  * @param ptr pointer to memory to free
00071  */
00072 
00073 void _stp_vfree(void *ptr)
00074 {
00075         if (likely(ptr))
00076                 vfree(ptr);
00077 }