mardyk01 | 7b51dbe | 2024-01-17 15:25:36 -0600 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2024, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * FIFO for matching strings to integers |
| 9 | */ |
| 10 | |
| 11 | #include "nfifo.h" |
| 12 | |
| 13 | #ifdef SMC_FUZZ_TMALLOC |
| 14 | #define GENMALLOC(x) malloc((x)) |
| 15 | #define GENFREE(x) free((x)) |
| 16 | #else |
| 17 | #define GENMALLOC(x) smcmalloc((x), mmod) |
| 18 | #define GENFREE(x) smcfree((x), mmod) |
| 19 | #endif |
| 20 | |
| 21 | /* |
| 22 | * Initialization of FIFO |
| 23 | */ |
| 24 | void nfifoinit(struct nfifo *nf, struct memmod *mmod) |
| 25 | { |
| 26 | nf->nent = 0; |
| 27 | nf->thent = NFIFO_Q_THRESHOLD; |
| 28 | nf->lnme = GENMALLOC(nf->thent * sizeof(char *)); |
| 29 | } |
| 30 | |
| 31 | /* |
| 32 | * push string to FIFO for automatic numerical assignment |
| 33 | */ |
| 34 | void pushnme(char *nme, struct nfifo *nf, struct memmod *mmod) |
| 35 | { |
| 36 | char **tnme; |
| 37 | |
| 38 | if (searchnme(nme, nf, mmod) == -1) { |
| 39 | if (nf->nent >= nf->thent) { |
| 40 | nf->thent += NFIFO_Q_THRESHOLD; |
| 41 | tnme = GENMALLOC(nf->thent * sizeof(char *)); |
| 42 | for (unsigned int x = 0; x < nf->nent; x++) { |
| 43 | tnme[x] = GENMALLOC(1 * sizeof(char[MAX_NAME_CHARS])); |
| 44 | strlcpy(tnme[x], nf->lnme[x], MAX_NAME_CHARS); |
| 45 | } |
| 46 | tnme[nf->nent] = GENMALLOC(1 * sizeof(char[MAX_NAME_CHARS])); |
| 47 | strlcpy(tnme[nf->nent], nme, MAX_NAME_CHARS); |
| 48 | for (unsigned int x = 0; x < nf->nent; x++) { |
| 49 | GENFREE(nf->lnme[x]); |
| 50 | } |
| 51 | GENFREE(nf->lnme); |
| 52 | nf->lnme = tnme; |
| 53 | } else { |
| 54 | nf->lnme[nf->nent] = GENMALLOC(1 * sizeof(char[MAX_NAME_CHARS])); |
| 55 | strlcpy(nf->lnme[nf->nent], nme, MAX_NAME_CHARS); |
| 56 | } |
| 57 | nf->nent++; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * Find name associated with numercal designation |
| 63 | */ |
| 64 | char *readnme(int ent, struct nfifo *nf, struct memmod *mmod) |
| 65 | { |
| 66 | return nf->lnme[ent]; |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Search FIFO for integer given an input string returns -1 |
| 71 | * if not found |
| 72 | */ |
| 73 | int searchnme(char *nme, struct nfifo *nf, struct memmod *mmod) |
| 74 | { |
| 75 | for (unsigned int x = 0; x < nf->nent; x++) { |
| 76 | if (strcmp(nf->lnme[x], nme) == CMP_SUCCESS) { |
| 77 | return (x + 1); |
| 78 | } |
| 79 | } |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Print of all elements of FIFO string and associated integer |
| 85 | */ |
| 86 | void printent(struct nfifo *nf) |
| 87 | { |
| 88 | for (unsigned int x = 0; x < nf->nent; x++) { |
| 89 | printf("nfifo entry %s has value %d\n", nf->lnme[x], x); |
| 90 | } |
| 91 | } |