maug
Quick and dirty C mini-augmentation library.
Loading...
Searching...
No Matches
mmem.h
Go to the documentation of this file.
1
2#ifndef MMEM_H
3#define MMEM_H
4
11#if defined( MAUG_OS_PALM )
12
13/* TODO */
14# pragma message( "warning: not implemented!" )
15
16#elif defined( MAUG_OS_DOS_REAL )
17
18typedef void* MAUG_MHANDLE;
19
20/* TODO: These need refinement/tuning for DOS low-memory/segments! */
21# define maug_malloc( nmemb, sz ) (void*)malloc( sz * nmemb )
22
27# define maug_mrealloc( handle, nmemb, sz ) \
28 (void*)realloc( handle, (sz) * (nmemb) )
29
30# define maug_mzero( ptr, sz ) memset( ptr, '\0', sz )
31
32# define maug_mcpy( ptr_dest, ptr_src, sz ) memcpy( ptr_dest, ptr_src, sz )
33
34# define maug_mfree( handle ) free( handle ); handle = NULL;
35
36# define maug_mlock( handle, ptr ) ptr = handle; handle = NULL;
37
38# define maug_munlock( handle, ptr ) handle = ptr; ptr = NULL;
39
40# define maug_strncpy( dest, src, len ) strncpy( dest, src, len )
41
42# define maug_strlen( str ) strlen( str )
43
44#else
45
46typedef void* MAUG_MHANDLE;
47
48# define maug_malloc( nmemb, sz ) (void*)malloc( sz * nmemb )
49
54# define maug_mrealloc( handle, nmemb, sz ) \
55 (void*)realloc( handle, (sz) * (nmemb) )
56
62# define maug_mzero( ptr, sz ) memset( ptr, '\0', sz )
63
64# define maug_mcpy( ptr_dest, ptr_src, sz ) memcpy( ptr_dest, ptr_src, sz )
65
66# define maug_mfree( handle ) free( handle ); handle = NULL;
67
68# define maug_mlock( handle, ptr ) ptr = handle; handle = NULL;
69
70# define maug_munlock( handle, ptr ) handle = ptr; ptr = NULL;
71
72# define maug_strncpy( dest, src, len ) strncpy( dest, src, len )
73
74# define maug_strlen( str ) strlen( str )
75
76#endif
77
78#define maug_mrealloc_test( new_handle, handle, nmemb, sz ) \
79 maug_cleanup_if_lt_overflow( (sz) * (nmemb), sz ); \
80 new_handle = maug_mrealloc( handle, nmemb, sz ); \
81 maug_cleanup_if_null_alloc( MAUG_MHANDLE, new_handle ); \
82 handle = new_handle;
83
84char* maug_strchr( const char* str, char c );
85
86char* maug_strrchr( const char* str, char c );
87
88 /* maug_mem */
89
90#ifdef MMEM_C
91
92char* maug_strchr( const char* str, char c ) {
93 size_t str_sz = 0,
94 i = 0;
95
96 str_sz = maug_strlen( str );
97
98 for( i = 0 ; str_sz > i ; i++ ) {
99 if( str[i] == c ) {
100 return (char*)&(str[i]);
101 }
102 }
103
104 return NULL;
105}
106
107char* maug_strrchr( const char* str, char c ) {
108 ssize_t str_sz = 0,
109 i = 0;
110
111 str_sz = maug_strlen( str );
112
113 for( i = str_sz ; 0 <= i ; i-- ) {
114 if( str[i] == c ) {
115 return (char*)&(str[i]);
116 }
117 }
118
119 return NULL;
120}
121
122#endif /* MMEM_C */
123
124#endif /* !MMEM_H */
125