aboutsummaryrefslogtreecommitdiffstats
path: root/testhal/STM32/multi/SPI/main.c
diff options
context:
space:
mode:
authoredolomb <none@example.com>2017-10-30 09:12:43 +0000
committeredolomb <none@example.com>2017-10-30 09:12:43 +0000
commiteb6b370fa670d26adb67a50959f82607ad1ebe27 (patch)
tree4207870f439c679140e2ebd19226553b5b8b7843 /testhal/STM32/multi/SPI/main.c
parent8f6afd211bbf59d084e66cebbadefde1e59bb119 (diff)
downloadChibiOS-eb6b370fa670d26adb67a50959f82607ad1ebe27.tar.gz
ChibiOS-eb6b370fa670d26adb67a50959f82607ad1ebe27.tar.bz2
ChibiOS-eb6b370fa670d26adb67a50959f82607ad1ebe27.zip
git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@10909 35acf78f-673a-0410-8e92-d51de3d6d3f4
Diffstat (limited to 'testhal/STM32/multi/SPI/main.c')
0 files changed, 0 insertions, 0 deletions
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
/*
 * Revision Control Information
 *
 * /projects/hsis/CVS/utilities/st/st.c,v
 * serdar
 * 1.1
 * 1993/07/29 01:00:13
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "st.h"

ABC_NAMESPACE_IMPL_START


#define ST_NUMCMP(x,y) ((x) != (y))
#define ST_NUMHASH(x,size) (Abc_AbsInt((long)x)%(size))
//#define ST_PTRHASH(x,size) ((int)((ABC_PTRUINT_T)(x)>>2)%size)  // 64-bit bug fix 9/17/2007
#define ST_PTRHASH(x,size) ((int)(((ABC_PTRUINT_T)(x)>>2)%size))
#define EQUAL(func, x, y) \
    ((((func) == st_numcmp) || ((func) == st_ptrcmp)) ?\
      (ST_NUMCMP((x),(y)) == 0) : ((*func)((x), (y)) == 0))


#define do_hash(key, table)\
    ((table->hash == st_ptrhash) ? ST_PTRHASH((key),(table)->num_bins) :\
     (table->hash == st_numhash) ? ST_NUMHASH((key), (table)->num_bins) :\
     (*table->hash)((key), (table)->num_bins))

static int rehash(st_table *table);

int st_numhash(const char*, int);
int st_ptrhash(const char*, int);
int st_numcmp(const char*, const char*);
int st_ptrcmp(const char*, const char*);

st_table *
st_init_table_with_params(st_compare_func_type compare, st_hash_func_type hash, int size, int density, double grow_factor, int reorder_flag)
{
    int i;
    st_table *newTable;

    newTable = ABC_ALLOC(st_table, 1);
    if (newTable == NULL) {
    return NULL;
    }
    newTable->compare = compare;
    newTable->hash = hash;
    newTable->num_entries = 0;
    newTable->max_density = density;
    newTable->grow_factor = grow_factor;
    newTable->reorder_flag = reorder_flag;
    if (size <= 0) {
    size = 1;
    }
    newTable->num_bins = size;
    newTable->bins = ABC_ALLOC(st_table_entry *, size);
    if (newTable->bins == NULL) {
    ABC_FREE(newTable);
    return NULL;
    }
    for(i = 0; i < size; i++) {
    newTable->bins[i] = 0;
    }
    return newTable;
}

st_table *
st_init_table(st_compare_func_type compare, st_hash_func_type hash)
{
    return st_init_table_with_params(compare, hash, ST_DEFAULT_INIT_TABLE_SIZE,
                     ST_DEFAULT_MAX_DENSITY,
                     ST_DEFAULT_GROW_FACTOR,
                     ST_DEFAULT_REORDER_FLAG);
}
                
void
st_free_table(st_table *table)
{
    st_table_entry *ptr, *next;
    int i;

    for(i = 0; i < table->num_bins ; i++) {
    ptr = table->bins[i];
    while (ptr != NULL) {
        next = ptr->next;
        ABC_FREE(ptr);
        ptr = next;
    }
    }
    ABC_FREE(table->bins);
    ABC_FREE(table);
}

#define PTR_NOT_EQUAL(table, ptr, user_key)\
(ptr != NULL && !EQUAL(table->compare, user_key, (ptr)->key))

#define FIND_ENTRY(table, hash_val, key, ptr, last) \
    (last) = &(table)->bins[hash_val];\
    (ptr) = *(last);\
    while (PTR_NOT_EQUAL((table), (ptr), (key))) {\
    (last) = &(ptr)->next; (ptr) = *(last);\
    }\
    if ((ptr) != NULL && (table)->reorder_flag) {\
    *(last) = (ptr)->next;\
    (ptr)->next = (table)->bins[hash_val];\
    (table)->bins[hash_val] = (ptr);\
    }

int
st_lookup(st_table *table, const char *key, char **value)
{
    int hash_val;
    st_table_entry *ptr, **last;

    hash_val = do_hash(key, table);

    FIND_ENTRY(table, hash_val, key, ptr, last);
    
    if (ptr == NULL) {
    return 0;
    } else {
    if (value != NULL) {
        *value = ptr->record; 
    }
    return 1;
    }
}

int
st_lookup_int(st_table *table, char *key, int *value)
{
    int hash_val;
    st_table_entry *ptr, **last;

    hash_val = do_hash(key, table);

    FIND_ENTRY(table, hash_val, key, ptr, last);
    
    if (ptr == NULL) {
    return 0;
    } else {
    if (value != 0) {
        *value = (long) ptr->record;
    }
    return 1;
    }
}

/* This macro does not check if memory allocation fails. Use at you own risk */
#define ADD_DIRECT(table, key, value, hash_val, new)\
{\
    if (table->num_entries/table->num_bins >= table->max_density) {\
    rehash(table);\
    hash_val = do_hash(key,table);\
    }\
    \
    new = ABC_ALLOC(st_table_entry, 1);\
    \
    new->key = key;\
    new->record = value;\
    new->next = table->bins[hash_val];\
    table->bins[hash_val] = new;\
    table->num_entries++;\
}

int
st_insert(st_table *table, const char *key, char *value)
{
    int hash_val;
    st_table_entry *newEntry;
    st_table_entry *ptr, **last;

    hash_val = do_hash(key, table);

    FIND_ENTRY(table, hash_val, key, ptr, last);

    if (ptr == NULL) {
    if (table->num_entries/table->num_bins >= table->max_density) {
        if (rehash(table) == ST_OUT_OF_MEM) {
        return ST_OUT_OF_MEM;
        }
        hash_val = do_hash(key, table);
    }
    newEntry = ABC_ALLOC(st_table_entry, 1);
    if (newEntry == NULL) {
        return ST_OUT_OF_MEM;
    }
    newEntry->key = (char *)key;
    newEntry->record = value;
    newEntry->next = table->bins[hash_val];
    table->bins[hash_val] = newEntry;
    table->num_entries++;
    return 0;
    } else {
    ptr->record = value;
    return 1;
    }
}

int
st_add_direct(st_table *table, char *key, char *value)
{
    int hash_val;
    st_table_entry *newEntry;
    
    hash_val = do_hash(key, table);
    if (table->num_entries / table->num_bins >= table->max_density) {
    if (rehash(table) == ST_OUT_OF_MEM) {
        return ST_OUT_OF_MEM;
    }
    }
    hash_val = do_hash(key, table);
    newEntry = ABC_ALLOC(st_table_entry, 1);
    if (newEntry == NULL) {
    return ST_OUT_OF_MEM;
    }
    newEntry->key = key;
    newEntry->record = value;
    newEntry->next = table->bins[hash_val];
    table->bins[hash_val] = newEntry;
    table->num_entries++;
    return 1;
}

int
st_find_or_add(st_table *table, char *key, char ***slot)
{
    int hash_val;
    st_table_entry *newEntry, *ptr, **last;

    hash_val = do_hash(key, table);

    FIND_ENTRY(table, hash_val, key, ptr, last);

    if (ptr == NULL) {
    if (table->num_entries / table->num_bins >= table->max_density) {
        if (rehash(table) == ST_OUT_OF_MEM) {
        return ST_OUT_OF_MEM;
        }
        hash_val = do_hash(key, table);
    }
    newEntry = ABC_ALLOC(st_table_entry, 1);
    if (newEntry == NULL) {
        return ST_OUT_OF_MEM;
    }
    newEntry->key = key;
    newEntry->record = (char *) 0;
    newEntry->next = table->bins[hash_val];
    table->bins[hash_val] = newEntry;
    table->num_entries++;
    if (slot != NULL) *slot = &newEntry->record;
    return 0;
    } else {
    if (slot != NULL) *slot = &ptr->record;
    return 1;
    }
}

int
st_find(st_table *table, char *key, char ***slot)
{
    int hash_val;
    st_table_entry *ptr, **last;

    hash_val = do_hash(key, table);

    FIND_ENTRY(table, hash_val, key, ptr, last);

    if (ptr == NULL) {
    return 0;
    } else {
    if (slot != NULL) {
        *slot = &ptr->record;
    }
    return 1;
    }
}

static int
rehash(st_table *table)
{
    st_table_entry *ptr, *next, **old_bins;
    int             i, old_num_bins, hash_val, old_num_entries;

    /* save old values */
    old_bins = table->bins;
    old_num_bins = table->num_bins;
    old_num_entries = table->num_entries;

    /* rehash */
    table->num_bins = (int)(table->grow_factor * old_num_bins);
    if (table->num_bins % 2 == 0) {
    table->num_bins += 1;
    }
    table->num_entries = 0;
    table->bins = ABC_ALLOC(st_table_entry *, table->num_bins);
    if (table->bins == NULL) {
    table->bins = old_bins;
    table->num_bins = old_num_bins;
    table->num_entries = old_num_entries;
    return ST_OUT_OF_MEM;
    }
    /* initialize */
    for (i = 0; i < table->num_bins; i++) {
    table->bins[i] = 0;
    }

    /* copy data over */
    for (i = 0; i < old_num_bins; i++) {
    ptr = old_bins[i];
    while (ptr != NULL) {
        next = ptr->next;
        hash_val = do_hash(ptr->key, table);
        ptr->next = table->bins[hash_val];
        table->bins[hash_val] = ptr;
        table->num_entries++;
        ptr = next;
    }
    }
    ABC_FREE(old_bins);

    return 1;
}

st_table *
st_copy(st_table *old_table)
{
    st_table *newEntry_table;
    st_table_entry *ptr, *newEntryptr, *next, *newEntry;
    int i, j, num_bins = old_table->num_bins;

    newEntry_table = ABC_ALLOC(st_table, 1);
    if (newEntry_table == NULL) {
    return NULL;
    }
    
    *newEntry_table = *old_table;
    newEntry_table->bins = ABC_ALLOC(st_table_entry *, num_bins);
    if (newEntry_table->bins == NULL) {
    ABC_FREE(newEntry_table);
    return NULL;
    }
    for(i = 0; i < num_bins ; i++) {
    newEntry_table->bins[i] = NULL;
    ptr = old_table->bins[i];
    while (ptr != NULL) {
        newEntry = ABC_ALLOC(st_table_entry, 1);
        if (newEntry == NULL) {
        for (j = 0; j <= i; j++) {
            newEntryptr = newEntry_table->bins[j];
            while (newEntryptr != NULL) {
            next = newEntryptr->next;
            ABC_FREE(newEntryptr);
            newEntryptr = next;
            }
        }
        ABC_FREE(newEntry_table->bins);
        ABC_FREE(newEntry_table);
        return NULL;
        }
        *newEntry = *ptr;
        newEntry->next = newEntry_table->bins[i];
        newEntry_table->bins[i] = newEntry;
        ptr = ptr->next;
    }
    }
    return newEntry_table;
}

int
st_delete(st_table *table, const char **keyp, char **value)
{
    int hash_val;
    const char *key = *keyp;
    st_table_entry *ptr, **last;

    hash_val = do_hash(key, table);

    FIND_ENTRY(table, hash_val, key, ptr ,last);
    
    if (ptr == NULL) {
    return 0;
    }

    *last = ptr->next;
    if (value != NULL) *value = ptr->record;
    *keyp = ptr->key;
    ABC_FREE(ptr);
    table->num_entries--;
    return 1;
}

int
st_delete_int(st_table *table, long *keyp, char **value)
{
    int hash_val;
    char *key = (char *) *keyp;
    st_table_entry *ptr, **last;

    hash_val = do_hash(key, table);

    FIND_ENTRY(table, hash_val, key, ptr ,last);

    if (ptr == NULL) {
        return 0;
    }

    *last = ptr->next;
    if (value != NULL) *value = ptr->record;
    *keyp = (long) ptr->key;
    ABC_FREE(ptr);
    table->num_entries--;
    return 1;
}

int
st_foreach(st_table *table, enum st_retval (*func)(char *, char *, char *), char *arg)