diff options
| author | 2021-09-13 20:28:16 +0200 | |
|---|---|---|
| committer | 2021-09-13 20:28:16 +0200 | |
| commit | 40b25fd572bf94d65e880a216bba5e825e5bbf2d (patch) | |
| tree | b6df709be2dbdec2186df1c7e2bec211b8d0dec5 /src | |
| parent | bcf56b30fea3b841ee11a9b4f3ac74f6de465f3d (diff) | |
| download | libirecovery-40b25fd572bf94d65e880a216bba5e825e5bbf2d.tar.gz libirecovery-40b25fd572bf94d65e880a216bba5e825e5bbf2d.tar.bz2 | |
Remove duplicated thread/collection code and use new libimobiledevice-glue instead
Diffstat (limited to 'src')
| -rw-r--r-- | src/Makefile.am | 7 | ||||
| -rw-r--r-- | src/libirecovery-1.0.pc.in | 2 | ||||
| -rw-r--r-- | src/libirecovery.c | 11 | ||||
| -rw-r--r-- | src/thread.c | 215 | ||||
| -rw-r--r-- | src/thread.h | 87 | ||||
| -rw-r--r-- | src/utils.c | 92 | ||||
| -rw-r--r-- | src/utils.h | 52 |
7 files changed, 9 insertions, 457 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 06a6374..f80ffa7 100644 --- a/src/Makefile.am +++ b/src/Makefile.am | |||
| @@ -3,19 +3,18 @@ AM_CPPFLAGS = -I$(top_srcdir)/include | |||
| 3 | AM_CFLAGS = \ | 3 | AM_CFLAGS = \ |
| 4 | $(GLOBAL_CFLAGS) \ | 4 | $(GLOBAL_CFLAGS) \ |
| 5 | $(LFS_CFLAGS) \ | 5 | $(LFS_CFLAGS) \ |
| 6 | $(limd_glue_CFLAGS) \ | ||
| 6 | $(libusb_CFLAGS) | 7 | $(libusb_CFLAGS) |
| 7 | 8 | ||
| 8 | AM_LDFLAGS = \ | 9 | AM_LDFLAGS = \ |
| 9 | $(GLOBAL_LDFLAGS) \ | 10 | $(GLOBAL_LDFLAGS) \ |
| 11 | $(limd_glue_LIBS) \ | ||
| 10 | $(libusb_LIBS) | 12 | $(libusb_LIBS) |
| 11 | 13 | ||
| 12 | lib_LTLIBRARIES = libirecovery-1.0.la | 14 | lib_LTLIBRARIES = libirecovery-1.0.la |
| 13 | libirecovery_1_0_la_CFLAGS = $(AM_CFLAGS) | 15 | libirecovery_1_0_la_CFLAGS = $(AM_CFLAGS) |
| 14 | libirecovery_1_0_la_LDFLAGS = $(AM_LDFLAGS) -version-info $(LIBIRECOVERY_SO_VERSION) -no-undefined | 16 | libirecovery_1_0_la_LDFLAGS = $(AM_LDFLAGS) -version-info $(LIBIRECOVERY_SO_VERSION) -no-undefined |
| 15 | libirecovery_1_0_la_SOURCES = \ | 17 | libirecovery_1_0_la_SOURCES = libirecovery.c |
| 16 | libirecovery.c \ | ||
| 17 | utils.c utils.h \ | ||
| 18 | thread.c thread.h | ||
| 19 | 18 | ||
| 20 | if WIN32 | 19 | if WIN32 |
| 21 | libirecovery_1_0_la_LDFLAGS += -avoid-version | 20 | libirecovery_1_0_la_LDFLAGS += -avoid-version |
diff --git a/src/libirecovery-1.0.pc.in b/src/libirecovery-1.0.pc.in index 25144da..e6c94a3 100644 --- a/src/libirecovery-1.0.pc.in +++ b/src/libirecovery-1.0.pc.in | |||
| @@ -8,4 +8,4 @@ Description: A library to communicate with iBoot/iBSS on iOS devices via USB | |||
| 8 | Version: @PACKAGE_VERSION@ | 8 | Version: @PACKAGE_VERSION@ |
| 9 | Libs: -L${libdir} -lirecovery-1.0 | 9 | Libs: -L${libdir} -lirecovery-1.0 |
| 10 | Cflags: -I${includedir} | 10 | Cflags: -I${includedir} |
| 11 | Requires.private: @LIBUSB_REQUIRED@ | 11 | Requires.private: libimobiledevice-glue-1.0 >= @LIMD_GLUE_VERSION@ @LIBUSB_REQUIRED@ |
diff --git a/src/libirecovery.c b/src/libirecovery.c index 2219f36..0446e01 100644 --- a/src/libirecovery.c +++ b/src/libirecovery.c | |||
| @@ -32,11 +32,13 @@ | |||
| 32 | #include <unistd.h> | 32 | #include <unistd.h> |
| 33 | #include <sys/stat.h> | 33 | #include <sys/stat.h> |
| 34 | 34 | ||
| 35 | #include <libimobiledevice-glue/collection.h> | ||
| 36 | #include <libimobiledevice-glue/thread.h> | ||
| 37 | |||
| 35 | #ifndef USE_DUMMY | 38 | #ifndef USE_DUMMY |
| 36 | #ifndef WIN32 | 39 | #ifndef WIN32 |
| 37 | #ifndef HAVE_IOKIT | 40 | #ifndef HAVE_IOKIT |
| 38 | #include <libusb.h> | 41 | #include <libusb.h> |
| 39 | #include <pthread.h> | ||
| 40 | #if (defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01000102)) || (defined(LIBUSBX_API_VERSION) && (LIBUSBX_API_VERSION >= 0x01000102)) | 42 | #if (defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01000102)) || (defined(LIBUSBX_API_VERSION) && (LIBUSBX_API_VERSION >= 0x01000102)) |
| 41 | #define HAVE_LIBUSB_HOTPLUG_API 1 | 43 | #define HAVE_LIBUSB_HOTPLUG_API 1 |
| 42 | #endif | 44 | #endif |
| @@ -44,7 +46,6 @@ | |||
| 44 | #include <CoreFoundation/CoreFoundation.h> | 46 | #include <CoreFoundation/CoreFoundation.h> |
| 45 | #include <IOKit/usb/IOUSBLib.h> | 47 | #include <IOKit/usb/IOUSBLib.h> |
| 46 | #include <IOKit/IOCFPlugIn.h> | 48 | #include <IOKit/IOCFPlugIn.h> |
| 47 | #include <pthread.h> | ||
| 48 | #endif | 49 | #endif |
| 49 | #else | 50 | #else |
| 50 | #define WIN32_LEAN_AND_MEAN | 51 | #define WIN32_LEAN_AND_MEAN |
| @@ -67,8 +68,6 @@ | |||
| 67 | #endif | 68 | #endif |
| 68 | 69 | ||
| 69 | #include "libirecovery.h" | 70 | #include "libirecovery.h" |
| 70 | #include "utils.h" | ||
| 71 | #include "thread.h" | ||
| 72 | 71 | ||
| 73 | struct irecv_client_private { | 72 | struct irecv_client_private { |
| 74 | int debug; | 73 | int debug; |
| @@ -2977,8 +2976,8 @@ IRECV_API irecv_error_t irecv_trigger_limera1n_exploit(irecv_client_t client) { | |||
| 2977 | // which can be accomplished by sending on another thread. | 2976 | // which can be accomplished by sending on another thread. |
| 2978 | 2977 | ||
| 2979 | void *args[2] = { client->handle, &req }; | 2978 | void *args[2] = { client->handle, &req }; |
| 2980 | pthread_t thread; | 2979 | THREAD_T thread; |
| 2981 | pthread_create(&thread, NULL, iokit_limera1n_usb_submit_request, args); | 2980 | thread_new(&thread, iokit_limera1n_usb_submit_request, args); |
| 2982 | 2981 | ||
| 2983 | usleep(5 * 1000); | 2982 | usleep(5 * 1000); |
| 2984 | result = (*client->handle)->USBDeviceAbortPipeZero(client->handle); | 2983 | result = (*client->handle)->USBDeviceAbortPipeZero(client->handle); |
diff --git a/src/thread.c b/src/thread.c deleted file mode 100644 index 796ea0b..0000000 --- a/src/thread.c +++ /dev/null | |||
| @@ -1,215 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * thread.c | ||
| 3 | * | ||
| 4 | * Copyright (c) 2012-2019 Nikias Bassen, All Rights Reserved. | ||
| 5 | * Copyright (c) 2012 Martin Szulecki, All Rights Reserved. | ||
| 6 | * | ||
| 7 | * This library is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * This library is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with this library; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | #ifdef HAVE_CONFIG_H | ||
| 23 | #include <config.h> | ||
| 24 | #endif | ||
| 25 | #include "thread.h" | ||
| 26 | |||
| 27 | int thread_new(THREAD_T *thread, thread_func_t thread_func, void* data) | ||
| 28 | { | ||
| 29 | #ifdef WIN32 | ||
| 30 | HANDLE th = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread_func, data, 0, NULL); | ||
| 31 | if (th == NULL) { | ||
| 32 | return -1; | ||
| 33 | } | ||
| 34 | *thread = th; | ||
| 35 | return 0; | ||
| 36 | #else | ||
| 37 | int res = pthread_create(thread, NULL, thread_func, data); | ||
| 38 | return res; | ||
| 39 | #endif | ||
| 40 | } | ||
| 41 | |||
| 42 | void thread_detach(THREAD_T thread) | ||
| 43 | { | ||
| 44 | #ifdef WIN32 | ||
| 45 | CloseHandle(thread); | ||
| 46 | #else | ||
| 47 | pthread_detach(thread); | ||
| 48 | #endif | ||
| 49 | } | ||
| 50 | |||
| 51 | void thread_free(THREAD_T thread) | ||
| 52 | { | ||
| 53 | #ifdef WIN32 | ||
| 54 | CloseHandle(thread); | ||
| 55 | #endif | ||
| 56 | } | ||
| 57 | |||
| 58 | int thread_join(THREAD_T thread) | ||
| 59 | { | ||
| 60 | /* wait for thread to complete */ | ||
| 61 | #ifdef WIN32 | ||
| 62 | return (int)WaitForSingleObject(thread, INFINITE); | ||
| 63 | #else | ||
| 64 | return pthread_join(thread, NULL); | ||
| 65 | #endif | ||
| 66 | } | ||
| 67 | |||
| 68 | int thread_alive(THREAD_T thread) | ||
| 69 | { | ||
| 70 | if (!thread) | ||
| 71 | return 0; | ||
| 72 | #ifdef WIN32 | ||
| 73 | return WaitForSingleObject(thread, 0) == WAIT_TIMEOUT; | ||
| 74 | #else | ||
| 75 | return pthread_kill(thread, 0) == 0; | ||
| 76 | #endif | ||
| 77 | } | ||
| 78 | |||
| 79 | int thread_cancel(THREAD_T thread) | ||
| 80 | { | ||
| 81 | #ifdef WIN32 | ||
| 82 | return -1; | ||
| 83 | #else | ||
| 84 | #ifdef HAVE_PTHREAD_CANCEL | ||
| 85 | return pthread_cancel(thread); | ||
| 86 | #else | ||
| 87 | return -1; | ||
| 88 | #endif | ||
| 89 | #endif | ||
| 90 | } | ||
| 91 | |||
| 92 | void mutex_init(mutex_t* mutex) | ||
| 93 | { | ||
| 94 | #ifdef WIN32 | ||
| 95 | InitializeCriticalSection(mutex); | ||
| 96 | #else | ||
| 97 | pthread_mutex_init(mutex, NULL); | ||
| 98 | #endif | ||
| 99 | } | ||
| 100 | |||
| 101 | void mutex_destroy(mutex_t* mutex) | ||
| 102 | { | ||
| 103 | #ifdef WIN32 | ||
| 104 | DeleteCriticalSection(mutex); | ||
| 105 | #else | ||
| 106 | pthread_mutex_destroy(mutex); | ||
| 107 | #endif | ||
| 108 | } | ||
| 109 | |||
| 110 | void mutex_lock(mutex_t* mutex) | ||
| 111 | { | ||
| 112 | #ifdef WIN32 | ||
| 113 | EnterCriticalSection(mutex); | ||
| 114 | #else | ||
| 115 | pthread_mutex_lock(mutex); | ||
| 116 | #endif | ||
| 117 | } | ||
| 118 | |||
| 119 | void mutex_unlock(mutex_t* mutex) | ||
| 120 | { | ||
| 121 | #ifdef WIN32 | ||
| 122 | LeaveCriticalSection(mutex); | ||
| 123 | #else | ||
| 124 | pthread_mutex_unlock(mutex); | ||
| 125 | #endif | ||
| 126 | } | ||
| 127 | |||
| 128 | void thread_once(thread_once_t *once_control, void (*init_routine)(void)) | ||
| 129 | { | ||
| 130 | #ifdef WIN32 | ||
| 131 | while (InterlockedExchange(&(once_control->lock), 1) != 0) { | ||
| 132 | Sleep(1); | ||
| 133 | } | ||
| 134 | if (!once_control->state) { | ||
| 135 | once_control->state = 1; | ||
| 136 | init_routine(); | ||
| 137 | } | ||
| 138 | InterlockedExchange(&(once_control->lock), 0); | ||
| 139 | #else | ||
| 140 | pthread_once(once_control, init_routine); | ||
| 141 | #endif | ||
| 142 | } | ||
| 143 | |||
| 144 | void cond_init(cond_t* cond) | ||
| 145 | { | ||
| 146 | #ifdef WIN32 | ||
| 147 | cond->sem = CreateSemaphore(NULL, 0, 32767, NULL); | ||
| 148 | #else | ||
| 149 | pthread_cond_init(cond, NULL); | ||
| 150 | #endif | ||
| 151 | } | ||
| 152 | |||
| 153 | void cond_destroy(cond_t* cond) | ||
| 154 | { | ||
| 155 | #ifdef WIN32 | ||
| 156 | CloseHandle(cond->sem); | ||
| 157 | #else | ||
| 158 | pthread_cond_destroy(cond); | ||
| 159 | #endif | ||
| 160 | } | ||
| 161 | |||
| 162 | int cond_signal(cond_t* cond) | ||
| 163 | { | ||
| 164 | #ifdef WIN32 | ||
| 165 | int result = 0; | ||
| 166 | if (!ReleaseSemaphore(cond->sem, 1, NULL)) { | ||
| 167 | result = -1; | ||
| 168 | } | ||
| 169 | return result; | ||
| 170 | #else | ||
| 171 | return pthread_cond_signal(cond); | ||
| 172 | #endif | ||
| 173 | } | ||
| 174 | |||
| 175 | int cond_wait(cond_t* cond, mutex_t* mutex) | ||
| 176 | { | ||
| 177 | #ifdef WIN32 | ||
| 178 | mutex_unlock(mutex); | ||
| 179 | DWORD res = WaitForSingleObject(cond->sem, INFINITE); | ||
| 180 | switch (res) { | ||
| 181 | case WAIT_OBJECT_0: | ||
| 182 | return 0; | ||
| 183 | default: | ||
| 184 | return -1; | ||
| 185 | } | ||
| 186 | #else | ||
| 187 | return pthread_cond_wait(cond, mutex); | ||
| 188 | #endif | ||
| 189 | } | ||
| 190 | |||
| 191 | int cond_wait_timeout(cond_t* cond, mutex_t* mutex, unsigned int timeout_ms) | ||
| 192 | { | ||
| 193 | #ifdef WIN32 | ||
| 194 | mutex_unlock(mutex); | ||
| 195 | DWORD res = WaitForSingleObject(cond->sem, timeout_ms); | ||
| 196 | switch (res) { | ||
| 197 | case WAIT_OBJECT_0: | ||
| 198 | case WAIT_TIMEOUT: | ||
| 199 | return 0; | ||
| 200 | default: | ||
| 201 | return -1; | ||
| 202 | } | ||
| 203 | #else | ||
| 204 | struct timespec ts; | ||
| 205 | struct timeval now; | ||
| 206 | gettimeofday(&now, NULL); | ||
| 207 | |||
| 208 | ts.tv_sec = now.tv_sec + timeout_ms / 1000; | ||
| 209 | ts.tv_nsec = now.tv_usec * 1000 + 1000 * 1000 * (timeout_ms % 1000); | ||
| 210 | ts.tv_sec += ts.tv_nsec / (1000 * 1000 * 1000); | ||
| 211 | ts.tv_nsec %= (1000 * 1000 * 1000); | ||
| 212 | |||
| 213 | return pthread_cond_timedwait(cond, mutex, &ts); | ||
| 214 | #endif | ||
| 215 | } | ||
diff --git a/src/thread.h b/src/thread.h deleted file mode 100644 index 2aadc6e..0000000 --- a/src/thread.h +++ /dev/null | |||
| @@ -1,87 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * thread.h | ||
| 3 | * | ||
| 4 | * Copyright (c) 2012-2019 Nikias Bassen, All Rights Reserved. | ||
| 5 | * Copyright (c) 2012 Martin Szulecki, All Rights Reserved. | ||
| 6 | * | ||
| 7 | * This library is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * This library is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with this library; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | #ifndef __THREAD_H | ||
| 23 | #define __THREAD_H | ||
| 24 | |||
| 25 | #include <stddef.h> | ||
| 26 | |||
| 27 | #ifdef WIN32 | ||
| 28 | #include <windows.h> | ||
| 29 | typedef HANDLE THREAD_T; | ||
| 30 | typedef CRITICAL_SECTION mutex_t; | ||
| 31 | typedef struct { | ||
| 32 | HANDLE sem; | ||
| 33 | } cond_t; | ||
| 34 | typedef volatile struct { | ||
| 35 | LONG lock; | ||
| 36 | int state; | ||
| 37 | } thread_once_t; | ||
| 38 | #define THREAD_ONCE_INIT {0, 0} | ||
| 39 | #define THREAD_ID GetCurrentThreadId() | ||
| 40 | #define THREAD_T_NULL (THREAD_T)NULL | ||
| 41 | #else | ||
| 42 | #include <pthread.h> | ||
| 43 | #include <signal.h> | ||
| 44 | #include <sys/time.h> | ||
| 45 | typedef pthread_t THREAD_T; | ||
| 46 | typedef pthread_mutex_t mutex_t; | ||
| 47 | typedef pthread_cond_t cond_t; | ||
| 48 | typedef pthread_once_t thread_once_t; | ||
| 49 | #define THREAD_ONCE_INIT PTHREAD_ONCE_INIT | ||
| 50 | #define THREAD_ID pthread_self() | ||
| 51 | #define THREAD_T_NULL (THREAD_T)NULL | ||
| 52 | #endif | ||
| 53 | |||
| 54 | typedef void* (*thread_func_t)(void* data); | ||
| 55 | |||
| 56 | int thread_new(THREAD_T* thread, thread_func_t thread_func, void* data); | ||
| 57 | void thread_detach(THREAD_T thread); | ||
| 58 | void thread_free(THREAD_T thread); | ||
| 59 | int thread_join(THREAD_T thread); | ||
| 60 | int thread_alive(THREAD_T thread); | ||
| 61 | |||
| 62 | int thread_cancel(THREAD_T thread); | ||
| 63 | |||
| 64 | #ifdef WIN32 | ||
| 65 | #undef HAVE_THREAD_CLEANUP | ||
| 66 | #else | ||
| 67 | #ifdef HAVE_PTHREAD_CANCEL | ||
| 68 | #define HAVE_THREAD_CLEANUP 1 | ||
| 69 | #define thread_cleanup_push(routine, arg) pthread_cleanup_push(routine, arg) | ||
| 70 | #define thread_cleanup_pop(execute) pthread_cleanup_pop(execute) | ||
| 71 | #endif | ||
| 72 | #endif | ||
| 73 | |||
| 74 | void mutex_init(mutex_t* mutex); | ||
| 75 | void mutex_destroy(mutex_t* mutex); | ||
| 76 | void mutex_lock(mutex_t* mutex); | ||
| 77 | void mutex_unlock(mutex_t* mutex); | ||
| 78 | |||
| 79 | void thread_once(thread_once_t *once_control, void (*init_routine)(void)); | ||
| 80 | |||
| 81 | void cond_init(cond_t* cond); | ||
| 82 | void cond_destroy(cond_t* cond); | ||
| 83 | int cond_signal(cond_t* cond); | ||
| 84 | int cond_wait(cond_t* cond, mutex_t* mutex); | ||
| 85 | int cond_wait_timeout(cond_t* cond, mutex_t* mutex, unsigned int timeout_ms); | ||
| 86 | |||
| 87 | #endif | ||
diff --git a/src/utils.c b/src/utils.c deleted file mode 100644 index 826bfbe..0000000 --- a/src/utils.c +++ /dev/null | |||
| @@ -1,92 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * utils.c | ||
| 3 | * | ||
| 4 | * Copyright (C) 2009 Hector Martin <hector@marcansoft.com> | ||
| 5 | * Copyright (C) 2009 Nikias Bassen <nikias@gmx.li> | ||
| 6 | * Copyright (c) 2013 Federico Mena Quintero | ||
| 7 | * | ||
| 8 | * This library is free software; you can redistribute it and/or modify | ||
| 9 | * it under the terms of the GNU Lesser General Public License as | ||
| 10 | * published by the Free Software Foundation, either version 2.1 of the | ||
| 11 | * License, or (at your option) any later version. | ||
| 12 | * | ||
| 13 | * This library is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 16 | * GNU General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU Lesser General Public | ||
| 19 | * License along with this program; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 21 | */ | ||
| 22 | |||
| 23 | #ifdef HAVE_CONFIG_H | ||
| 24 | #include <config.h> | ||
| 25 | #endif | ||
| 26 | |||
| 27 | #include <stdlib.h> | ||
| 28 | #include <string.h> | ||
| 29 | #include <stdio.h> | ||
| 30 | #include <errno.h> | ||
| 31 | |||
| 32 | #include "utils.h" | ||
| 33 | |||
| 34 | #define CAPACITY_STEP 8 | ||
| 35 | |||
| 36 | void collection_init(struct collection *col) | ||
| 37 | { | ||
| 38 | col->list = malloc(sizeof(void *) * CAPACITY_STEP); | ||
| 39 | memset(col->list, 0, sizeof(void *) * CAPACITY_STEP); | ||
| 40 | col->capacity = CAPACITY_STEP; | ||
| 41 | } | ||
| 42 | |||
| 43 | void collection_free(struct collection *col) | ||
| 44 | { | ||
| 45 | free(col->list); | ||
| 46 | col->list = NULL; | ||
| 47 | col->capacity = 0; | ||
| 48 | } | ||
| 49 | |||
| 50 | void collection_add(struct collection *col, void *element) | ||
| 51 | { | ||
| 52 | int i; | ||
| 53 | for(i=0; i<col->capacity; i++) { | ||
| 54 | if(!col->list[i]) { | ||
| 55 | col->list[i] = element; | ||
| 56 | return; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | col->list = realloc(col->list, sizeof(void*) * (col->capacity + CAPACITY_STEP)); | ||
| 60 | memset(&col->list[col->capacity], 0, sizeof(void *) * CAPACITY_STEP); | ||
| 61 | col->list[col->capacity] = element; | ||
| 62 | col->capacity += CAPACITY_STEP; | ||
| 63 | } | ||
| 64 | |||
| 65 | void collection_remove(struct collection *col, void *element) | ||
| 66 | { | ||
| 67 | int i; | ||
| 68 | for(i=0; i<col->capacity; i++) { | ||
| 69 | if(col->list[i] == element) { | ||
| 70 | col->list[i] = NULL; | ||
| 71 | return; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | int collection_count(struct collection *col) | ||
| 77 | { | ||
| 78 | int i, cnt = 0; | ||
| 79 | for(i=0; i<col->capacity; i++) { | ||
| 80 | if(col->list[i]) | ||
| 81 | cnt++; | ||
| 82 | } | ||
| 83 | return cnt; | ||
| 84 | } | ||
| 85 | |||
| 86 | void collection_copy(struct collection *dest, struct collection *src) | ||
| 87 | { | ||
| 88 | if (!dest || !src) return; | ||
| 89 | dest->capacity = src->capacity; | ||
| 90 | dest->list = malloc(sizeof(void*) * src->capacity); | ||
| 91 | memcpy(dest->list, src->list, sizeof(void*) * src->capacity); | ||
| 92 | } | ||
diff --git a/src/utils.h b/src/utils.h deleted file mode 100644 index ccb3a09..0000000 --- a/src/utils.h +++ /dev/null | |||
| @@ -1,52 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * utils.h | ||
| 3 | * | ||
| 4 | * Copyright (C) 2009 Hector Martin <hector@marcansoft.com> | ||
| 5 | * Copyright (C) 2009 Nikias Bassen <nikias@gmx.li> | ||
| 6 | * | ||
| 7 | * This library is free software; you can redistribute it and/or modify | ||
| 8 | * it under the terms of the GNU Lesser General Public License as | ||
| 9 | * published by the Free Software Foundation, either version 2.1 of the | ||
| 10 | * License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * This library is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 | * GNU General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with this program; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | #ifndef UTILS_H | ||
| 23 | #define UTILS_H | ||
| 24 | |||
| 25 | struct collection { | ||
| 26 | void **list; | ||
| 27 | int capacity; | ||
| 28 | }; | ||
| 29 | |||
| 30 | void collection_init(struct collection *col); | ||
| 31 | void collection_add(struct collection *col, void *element); | ||
| 32 | void collection_remove(struct collection *col, void *element); | ||
| 33 | int collection_count(struct collection *col); | ||
| 34 | void collection_free(struct collection *col); | ||
| 35 | void collection_copy(struct collection *dest, struct collection *src); | ||
| 36 | |||
| 37 | #define MERGE_(a,b) a ## _ ## b | ||
| 38 | #define LABEL_(a,b) MERGE_(a, b) | ||
| 39 | #define UNIQUE_VAR(a) LABEL_(a, __LINE__) | ||
| 40 | |||
| 41 | #define FOREACH(var, col) \ | ||
| 42 | do { \ | ||
| 43 | int UNIQUE_VAR(_iter); \ | ||
| 44 | for(UNIQUE_VAR(_iter)=0; UNIQUE_VAR(_iter)<(col)->capacity; UNIQUE_VAR(_iter)++) { \ | ||
| 45 | if(!(col)->list[UNIQUE_VAR(_iter)]) continue; \ | ||
| 46 | var = (col)->list[UNIQUE_VAR(_iter)]; | ||
| 47 | |||
| 48 | #define ENDFOREACH \ | ||
| 49 | } \ | ||
| 50 | } while(0); | ||
| 51 | |||
| 52 | #endif | ||
