The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include "symtab.h" |
| 20 | |
| 21 | #define DEFAULT_TABLE_SIZE 16 |
| 22 | |
| 23 | typedef struct { |
| 24 | char *symbol; |
| 25 | const void *cookie; |
| 26 | unsigned int flags; |
| 27 | } SymbolTableEntry; |
| 28 | |
| 29 | struct SymbolTable { |
| 30 | SymbolTableEntry *table; |
| 31 | int numEntries; |
| 32 | int maxSize; |
| 33 | }; |
| 34 | |
| 35 | SymbolTable * |
| 36 | createSymbolTable() |
| 37 | { |
| 38 | SymbolTable *tab; |
| 39 | |
| 40 | tab = (SymbolTable *)malloc(sizeof(SymbolTable)); |
| 41 | if (tab != NULL) { |
| 42 | tab->numEntries = 0; |
| 43 | tab->maxSize = DEFAULT_TABLE_SIZE; |
| 44 | tab->table = (SymbolTableEntry *)malloc( |
| 45 | tab->maxSize * sizeof(SymbolTableEntry)); |
| 46 | if (tab->table == NULL) { |
| 47 | free(tab); |
| 48 | tab = NULL; |
| 49 | } |
| 50 | } |
| 51 | return tab; |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | deleteSymbolTable(SymbolTable *tab) |
| 56 | { |
| 57 | if (tab != NULL) { |
| 58 | while (tab->numEntries > 0) { |
| 59 | free(tab->table[--tab->numEntries].symbol); |
| 60 | } |
| 61 | free(tab->table); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | void * |
| 66 | findInSymbolTable(SymbolTable *tab, const char *symbol, unsigned int flags) |
| 67 | { |
| 68 | int i; |
| 69 | |
| 70 | if (tab == NULL || symbol == NULL) { |
| 71 | return NULL; |
| 72 | } |
| 73 | |
| 74 | // TODO: Sort the table and binary search |
| 75 | for (i = 0; i < tab->numEntries; i++) { |
| 76 | if (strcmp(tab->table[i].symbol, symbol) == 0 && |
| 77 | tab->table[i].flags == flags) |
| 78 | { |
| 79 | return (void *)tab->table[i].cookie; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return NULL; |
| 84 | } |
| 85 | |
| 86 | int |
| 87 | addToSymbolTable(SymbolTable *tab, const char *symbol, unsigned int flags, |
| 88 | const void *cookie) |
| 89 | { |
| 90 | if (tab == NULL || symbol == NULL || cookie == NULL) { |
| 91 | return -1; |
| 92 | } |
| 93 | |
| 94 | /* Make sure that this symbol isn't already in the table. |
| 95 | */ |
| 96 | if (findInSymbolTable(tab, symbol, flags) != NULL) { |
| 97 | return -2; |
| 98 | } |
| 99 | |
| 100 | /* Make sure there's enough space for the new entry. |
| 101 | */ |
| 102 | if (tab->numEntries == tab->maxSize) { |
| 103 | SymbolTableEntry *newTable; |
| 104 | int newSize; |
| 105 | |
| 106 | newSize = tab->numEntries * 2; |
| 107 | if (newSize < DEFAULT_TABLE_SIZE) { |
| 108 | newSize = DEFAULT_TABLE_SIZE; |
| 109 | } |
| 110 | newTable = (SymbolTableEntry *)realloc(tab->table, |
| 111 | newSize * sizeof(SymbolTableEntry)); |
| 112 | if (newTable == NULL) { |
| 113 | return -1; |
| 114 | } |
| 115 | tab->maxSize = newSize; |
| 116 | tab->table = newTable; |
| 117 | } |
| 118 | |
| 119 | /* Insert the new entry. |
| 120 | */ |
| 121 | symbol = strdup(symbol); |
| 122 | if (symbol == NULL) { |
| 123 | return -1; |
| 124 | } |
| 125 | // TODO: Sort the table |
| 126 | tab->table[tab->numEntries].symbol = (char *)symbol; |
| 127 | tab->table[tab->numEntries].cookie = cookie; |
| 128 | tab->table[tab->numEntries].flags = flags; |
| 129 | tab->numEntries++; |
| 130 | |
| 131 | return 0; |
| 132 | } |