Changeset 71b00dcc in mainline for uspace/srv/net/structures/generic_field.h
- Timestamp:
- 2010-03-07T22:51:38Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 60ab6c3
- Parents:
- b5cbff4 (diff), 31c80a5 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/structures/generic_field.h
rb5cbff4 r71b00dcc 51 51 * @param[in] type Inner object type. 52 52 */ 53 #define GENERIC_FIELD_DECLARE( name, type )\54 \55 typedef struct name name##_t; \56 typedef name##_t * name##_ref; \57 \58 struct name{ \59 int size;\60 int next;\61 type ** items; \62 int magic;\63 }; \64 \65 int name##_add( name##_ref field, type * value );\66 int name##_count( name##_ref field );\67 void name##_destroy( name##_ref field );\68 void name##_exclude_index( name##_ref field, int index );\69 type ** name##_get_field( name##_ref field );\70 type * name##_get_index( name##_ref field, int index );\71 int name##_initialize( name##_ref field );\72 int name##_is_valid( name##_ref field);53 #define GENERIC_FIELD_DECLARE(name, type) \ 54 \ 55 typedef struct name name##_t; \ 56 typedef name##_t * name##_ref; \ 57 \ 58 struct name{ \ 59 int size; \ 60 int next; \ 61 type ** items; \ 62 int magic; \ 63 }; \ 64 \ 65 int name##_add(name##_ref field, type * value); \ 66 int name##_count(name##_ref field); \ 67 void name##_destroy(name##_ref field); \ 68 void name##_exclude_index(name##_ref field, int index); \ 69 type ** name##_get_field(name##_ref field); \ 70 type * name##_get_index(name##_ref field, int index); \ 71 int name##_initialize(name##_ref field); \ 72 int name##_is_valid(name##_ref field); 73 73 74 74 /** Generic type field implementation. … … 77 77 * @param[in] type Inner object type. 78 78 */ 79 #define GENERIC_FIELD_IMPLEMENT( name, type ) \ 80 \ 81 int name##_add( name##_ref field, type * value ){ \ 82 if( name##_is_valid( field )){ \ 83 if( field->next == ( field->size - 1 )){ \ 84 type ** tmp; \ 85 \ 86 tmp = ( type ** ) realloc( field->items, sizeof( type * ) * 2 * field->size ); \ 87 if( ! tmp ) return ENOMEM; \ 88 field->size *= 2; \ 89 field->items = tmp; \ 90 } \ 91 field->items[ field->next ] = value; \ 92 ++ field->next; \ 93 field->items[ field->next ] = NULL; \ 94 return field->next - 1; \ 95 } \ 96 return EINVAL; \ 97 } \ 98 \ 99 int name##_count( name##_ref field ){ \ 100 return name##_is_valid( field ) ? field->next : -1; \ 101 } \ 102 \ 103 void name##_destroy( name##_ref field ){ \ 104 if( name##_is_valid( field )){ \ 105 int index; \ 106 \ 107 field->magic = 0; \ 108 for( index = 0; index < field->next; ++ index ){ \ 109 if( field->items[ index ] ) free( field->items[ index ] ); \ 110 } \ 111 free( field->items ); \ 112 } \ 113 } \ 114 \ 115 void name##_exclude_index( name##_ref field, int index ){ \ 116 if( name##_is_valid( field ) && ( index >= 0 ) && ( index < field->next ) && ( field->items[ index ] )){ \ 117 free( field->items[ index ] ); \ 118 field->items[ index ] = NULL; \ 119 } \ 120 } \ 121 \ 122 type * name##_get_index( name##_ref field, int index ){ \ 123 if( name##_is_valid( field ) && ( index >= 0 ) && ( index < field->next ) && ( field->items[ index ] )){ \ 124 return field->items[ index ]; \ 125 } \ 126 return NULL; \ 127 } \ 128 \ 129 type ** name##_get_field( name##_ref field ){ \ 130 return name##_is_valid( field ) ? field->items : NULL; \ 131 } \ 132 \ 133 int name##_initialize( name##_ref field ){ \ 134 if( ! field ) return EINVAL; \ 135 field->size = 2; \ 136 field->next = 0; \ 137 field->items = ( type ** ) malloc( sizeof( type * ) * field->size ); \ 138 if( ! field->items ) return ENOMEM; \ 139 field->items[ field->next ] = NULL; \ 140 field->magic = GENERIC_FIELD_MAGIC_VALUE; \ 141 return EOK; \ 142 } \ 143 \ 144 int name##_is_valid( name##_ref field ){ \ 145 return field && ( field->magic == GENERIC_FIELD_MAGIC_VALUE ); \ 79 #define GENERIC_FIELD_IMPLEMENT(name, type) \ 80 \ 81 int name##_add(name##_ref field, type * value){ \ 82 if(name##_is_valid(field)){ \ 83 if(field->next == (field->size - 1)){ \ 84 type ** tmp; \ 85 \ 86 tmp = (type **) realloc(field->items, sizeof(type *) * 2 * field->size); \ 87 if(! tmp){ \ 88 return ENOMEM; \ 89 } \ 90 field->size *= 2; \ 91 field->items = tmp; \ 92 } \ 93 field->items[field->next] = value; \ 94 ++ field->next; \ 95 field->items[field->next] = NULL; \ 96 return field->next - 1; \ 97 } \ 98 return EINVAL; \ 99 } \ 100 \ 101 int name##_count(name##_ref field){ \ 102 return name##_is_valid(field) ? field->next : -1; \ 103 } \ 104 \ 105 void name##_destroy(name##_ref field){ \ 106 if(name##_is_valid(field)){ \ 107 int index; \ 108 \ 109 field->magic = 0; \ 110 for(index = 0; index < field->next; ++ index){ \ 111 if(field->items[index]){ \ 112 free(field->items[index]); \ 113 } \ 114 } \ 115 free(field->items); \ 116 } \ 117 } \ 118 \ 119 void name##_exclude_index(name##_ref field, int index){ \ 120 if(name##_is_valid(field) && (index >= 0) && (index < field->next) && (field->items[index])){ \ 121 free(field->items[index]); \ 122 field->items[index] = NULL; \ 123 } \ 124 } \ 125 \ 126 type * name##_get_index(name##_ref field, int index){ \ 127 if(name##_is_valid(field) && (index >= 0) && (index < field->next) && (field->items[index])){ \ 128 return field->items[index]; \ 129 } \ 130 return NULL; \ 131 } \ 132 \ 133 type ** name##_get_field(name##_ref field){ \ 134 return name##_is_valid(field) ? field->items : NULL; \ 135 } \ 136 \ 137 int name##_initialize(name##_ref field){ \ 138 if(! field){ \ 139 return EINVAL; \ 140 } \ 141 field->size = 2; \ 142 field->next = 0; \ 143 field->items = (type **) malloc(sizeof(type *) * field->size); \ 144 if(! field->items){ \ 145 return ENOMEM; \ 146 } \ 147 field->items[field->next] = NULL; \ 148 field->magic = GENERIC_FIELD_MAGIC_VALUE; \ 149 return EOK; \ 150 } \ 151 \ 152 int name##_is_valid(name##_ref field){ \ 153 return field && (field->magic == GENERIC_FIELD_MAGIC_VALUE); \ 146 154 } 147 155
Note:
See TracChangeset
for help on using the changeset viewer.