Глобальный массив структур, объявить размер в программе и использовать глобально, не передавая? - PullRequest
0 голосов
/ 26 ноября 2018

Просто для контекста, я делаю симулятор кеша как C-проект.Мне нужно объявить глобальный двойной массив структуры, установить размер этого глобального массива структуры в программе, а затем использовать этот глобальный массив в проекте.

Вот сокращенная версия того, чтоУ меня есть:

// Declare globals
int mapping = 0;
int offset = 0;
int index = 0;

char* allocation;
char* writePolicy;

struct cache {
    int validBit;
    int dirtyBit;
    unsigned int tag;
    unsigned int nextToReplace;
    };

void analyzeParameters(char* line, int lineNum) {

switch(lineNum) {
        case 1:
            mapping = atoi(line);
        case 2:
            offset = atoi(line);
        case 3:
            index = atoi(line);
        case 4:
            allocation = malloc(4);
            strcpy(allocation, line);
        case 5:
            writePolicy = malloc(4);
            strcpy(writePolicy, line);
    }

}

setupCache() {
int numSets = 1 << index;

struct cache cache[mapping][numSets];

printf("Declared cache struct with size %d ", numSets);
printf("num of ways %d\n", mapping);

// initialize bits in cache to 0

int j;
int i;

for (j = 0; j < mapping; j++) { 
    for (i = 0; i < numSets; i++) {
        cache[j][i].validBit = 0;
        cache[j][i].dirtyBit = 0;
    }
}

}

void hitChecker() {

for (d = 0; d < mapping; d++) {
    if (cache[d][index].validBit == 1) {
        if (cache[d][index].tag == tag) { 
            //found a hit

            if (type == "r") {
            // hit with a read instruction.
            rhits++;


            }
            else if (type == "w") {
            // hit with a write instruction
            whits++;

            }
        }

        else {
        // tag in cache index is not equal to tag being checked

            if (type == "r") {
            // missed with a read instruction.
            rmisses++;

            }
            else if (type == "w") {
            // missed with a write instruction
            wmisses++;

            }
        }
    }
    else {
    //cache is not valid
    printf("Cache has not been validated");
    }
}

void main(int argc, char**argv) {

analyzeParameters(passInEachLineOfFile, this works not important);

setupCache();

hitChecker();

}

Это работает, пока я не попытаюсь использовать структуру кэша.Я объявляю это глобально, устанавливаю размер в setUpCache, а затем в другом методе я хочу использовать этот двойной массив, объявленный глобально.Есть ли способ, которым я могу использовать его глобально или я должен передать структуру через параметры метода?

Ответы [ 2 ]

0 голосов
/ 26 ноября 2018

Чтобы иметь глобальную структуру кэша с определенными размерами во время выполнения, используйте:

int mapping, numsets;
struct cache **cache;

и:

void init(int nmaps, int nsets)
{
    cache=malloc(nmaps*sizeof(struct cache *));
    for (int i=0;i<nmaps;i++)
        cache[i]=malloc(nsets*sizeof(struct cache));
    mapping= nmaps;
    numsets= nsets;
}
0 голосов
/ 26 ноября 2018

Если вы хотите использовать cache в качестве глобальной переменной, объявите ее в глобальной области, и перед ее использованием.

struct cache {
    int validBit;
    int dirtyBit;
    unsigned int tag;
    unsigned int nextToReplace;
    };

struct cache cache[mapping][numSets];

Не очень хорошая идея ставитьпеременная в глобальной области видимости, если вы можете избежать этого.

Здесь, чтобы «инкапсулировать» функцию кэширования, я предлагаю использовать вместо static переменную в отдельный файл со всеми функциями, связанными с кэшем, чтобы поместить его в область действия "file" .Поскольку mapping, offset и index являются аргументами извне ваших функций кэширования, передайте их как параметры.

// cache-lib.c

// Declare the struct in the c file, it's only needed here
struct cache {
        int validBit;
        int dirtyBit;
        unsigned int tag;
        unsigned int nextToReplace;
        };

static struct cache cache[mapping][numSets];

// Stores below variable here, in the "file" scope.
static mapping, offset, index;

// Below cache functions. Declare them an includable header.

/// setupCache() definition. Since it needs mapping, offset and index which
/// are retrieved from the outside, you can pass them as parameter.

void setupCache(int mapping, int offset, int index) {
  //// [...]
}

/// hitChecker. Maybe you need here to store extra variables
/// as "file-scope" ones like index and mapping
void hitChecker() {
  //// [...]
}

Наконец, заголовок, который вы будете использовать везде, где вам нужен ваш кеш lib:

// cache-lib.h

void setupCache(int mapping, int offset, int index);
void hitChecker();

Вы также можете включить его в cache-lib.c, чтобы не беспокоиться о проблемах, связанных с порядком объявления функций.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...