Simple data structures / objects in plain C Snapshot
Functions
util.h File Reference
#include <cutils/base.h>
#include <ctype.h>

Go to the source code of this file.

Functions

M_INLINE void UTIL_swapbytes (unsigned char *src, unsigned char *dst, size_t size)
M_INLINE int UTIL_is_power_of_two (uint32_t num)
M_INLINE uint32_t UTIL_align (uint32_t num, uint32_t align)
M_INLINE void * UTIL_ptr_align (void *ptr, size_t align)
M_INLINE void * UTIL_ptr_page_start (void *ptr, size_t page_size)
M_INLINE int UTIL_ptr_is_aligned (void *ptr, size_t page_size)
M_INLINE int UTIL_ptr_in_range (void *ptr, void *from, void *to)
M_INLINE size_t UTIL_round_to_power_of_n (size_t num)
M_INLINE size_t UTIL_log_base_2_of_n (size_t num)
M_INLINE char * UTIL_skip_spaces (const char *p)
M_INLINE char * UTIL_skip_nspace (const char *p)

Function Documentation

M_INLINE uint32_t UTIL_align ( uint32_t  num,
uint32_t  align 
)

Definition at line 43 of file util.h.

{
        /* assert( UTIL_is_power_of_two( align ) ); */
        return (num  + align - 1 ) & (~(align - 1));
        /*
         * & (x) (~(align -1 )) set align to the closers alignment (x % align == 0) (round down to closest alignment)
         * num + align - 1      make sure that the argument crosses alignment border, 
         */
}
M_INLINE int UTIL_is_power_of_two ( uint32_t  num)

Definition at line 31 of file util.h.

{
        return (num & (num - 1)) == 0;
        /* num == 2 ^ n -1 | all ones
         * num == 2 ^ n    | one on next position after the last one.
         * & will return 0 (no overlap) if num is 2 ^ n
         */
}       
M_INLINE size_t UTIL_log_base_2_of_n ( size_t  num)

Definition at line 101 of file util.h.

{
        size_t ret = 0;

        while( num ) {
                num >>= 1;
                ret ++;
        }
        return ret;
}
M_INLINE void* UTIL_ptr_align ( void *  ptr,
size_t  align 
)

Definition at line 56 of file util.h.

{
        /* assert( UTIL_is_power_of_two( align ) ); */
        return (void *) ( (((ptrdiff_t) ptr) & ~(align - 1)) + align );
}
M_INLINE int UTIL_ptr_in_range ( void *  ptr,
void *  from,
void *  to 
)

Definition at line 85 of file util.h.

{
        return (uint8_t *) ptr >= (uint8_t *) from && (uint8_t *) ptr < (uint8_t *) to;
}
M_INLINE int UTIL_ptr_is_aligned ( void *  ptr,
size_t  page_size 
)

Definition at line 76 of file util.h.

{
        /* assert( UTIL_is_power_of_two( align ) ); */
        return ( ((ptrdiff_t) ptr) & ((ptrdiff_t) page_size - 1) ) == 0;
}
M_INLINE void* UTIL_ptr_page_start ( void *  ptr,
size_t  page_size 
)

Definition at line 64 of file util.h.

{
        /* assert( UTIL_is_power_of_two( align ) ); */
        return (void *) ( ((ptrdiff_t) ptr) & ~ ((ptrdiff_t) page_size - 1) );
        /*
         * round argument pointer down to the closes pagesize (assuming pagesize is power of two)
         */
}
M_INLINE size_t UTIL_round_to_power_of_n ( size_t  num)

Definition at line 91 of file util.h.

{
        size_t n = 1;

        while( n < num ) {
                n <<= 1;
        }
        return n;
}
M_INLINE char* UTIL_skip_nspace ( const char *  p)

Definition at line 121 of file util.h.

                                                {
        while( *p != '\0' && !isspace((int) *p)) {
                ++p;
        }
        return (char *) p;
}
M_INLINE char* UTIL_skip_spaces ( const char *  p)

Definition at line 114 of file util.h.

                                                {
        while( *p != '\0' && isspace((int) *p)) {
                ++p;
        }
        return (char *) p;
}
M_INLINE void UTIL_swapbytes ( unsigned char *  src,
unsigned char *  dst,
size_t  size 
)

Definition at line 13 of file util.h.

{
        unsigned char tmp;

        while(size>0) {
                tmp = *src;
                *src = *dst;
                *dst = tmp;

                ++src;
                ++dst;
                --size;
        }
}