diff --git a/src/mii_bank.c b/src/mii_bank.c index 51fcf9c..be21ccc 100644 --- a/src/mii_bank.c +++ b/src/mii_bank.c @@ -14,6 +14,23 @@ #include "mii.h" #include "mii_bank.h" +void +mii_bank_dispose( + mii_bank_t *bank) +{ + if (bank->alloc) + free(bank->mem); + bank->mem = NULL; + bank->alloc = 0; + if (bank->access) { + // Allow callback to free anything it wants + for (int i = 0; i < bank->size; i++) + if (bank->access[i].cb) + bank->access[i].cb(NULL, bank->access[i].param, 0, NULL, false); + free(bank->access); + } + bank->access = NULL; +} void mii_bank_write( diff --git a/src/mii_bank.h b/src/mii_bank.h index 4c7c509..8daa928 100644 --- a/src/mii_bank.h +++ b/src/mii_bank.h @@ -11,6 +11,18 @@ #include struct mii_bank_t; +/* + * Bank access callback can be register with banks and will be called + * when a specific page (or pages) are read/written to. + * Parameter will be the bank specified, the 'real' address beind read/written + * and the 'param' passed when the callback was registered. + * The callback should return true if it handled the access, false otherwise. + * If it return true, the bank will not be accessed for this read/write. + * + * Note: the callback will be called once with bank == NULL when the bank + * is being disposed, this allow any memory allocated by the callback to be + * freed. + */ typedef bool (*mii_bank_access_cb)( struct mii_bank_t *bank, void *param, @@ -33,6 +45,9 @@ typedef struct mii_bank_t { uint8_t *mem; } mii_bank_t; +void +mii_bank_dispose( + mii_bank_t *bank); void mii_bank_write( mii_bank_t *bank,