StoreEntry *allocateEntry(JNIEnv *pEnv, Store *pStore, jstring pKey) { int32_t lError = 0; StoreEntry *lEntry = findEntry(pEnv, pStore, pKey, &lError); if (lEntry != NULL) { releaseEntryValue(pEnv, lEntry); } else if (!lError) { if (pStore->mLength >= STORE_MAX_CAPACITY) { return NULL; } lEntry = pStore->mEntries + pStore->mLength; const char *lKeyTmp = (*pEnv)->GetStringUTFChars(pEnv, pKey, NULL); if (lKeyTmp == NULL) { return NULL; } lEntry->mKey = (char *) malloc(strlen(lKeyTmp)); strcpy(lEntry->mKey, lKeyTmp); (*pEnv)->ReleaseStringUTFChars(pEnv, pKey, lKeyTmp); ++pStore->mLength; } return lEntry; }
StoreEntry * allocateEntry(JNIEnv* pEnv, Store* pStore, jstring jstrKey) { // If entry already exists in the store, releases its content // and keep its key. int32_t lError = 0; StoreEntry* lEntry = findEntry(pEnv, pStore, jstrKey, &lError); if (NULL != lEntry) { releaseEntryValue(pEnv, lEntry); } // If entry does not exist, create a new entry right after // already allocated entries. else if (!lError) { // Checks store can accept a new entry. if (pStore->mLength >= STORE_MAX_CAPACITY) { throwStoreFullException(pEnv); return NULL; } lEntry = pStore->mEntries + pStore->mLength; // Copies the new key into its final C string buffer. const char* lKeyTmp = (*pEnv)->GetStringUTFChars(pEnv, jstrKey, NULL); if (NULL == lKeyTmp) { return NULL; } lEntry->mKey = (char*) malloc(strlen(lKeyTmp)); strcpy(lEntry->mKey, lKeyTmp); (*pEnv)->ReleaseStringUTFChars(pEnv, jstrKey, lKeyTmp); ++pStore->mLength; } return lEntry; }
JNIEXPORT void JNICALL Java_com_congnt_ndkguide_Store_finalizeStore( JNIEnv* pEnv, jobject pThis) { stopWatcher(pEnv, &mStoreWatcher); StoreEntry* lEntry = mStore.mEntries; StoreEntry* lEntryEnd = lEntry + mStore.mLength; while (lEntry < lEntryEnd) { free(lEntry->mKey); releaseEntryValue(pEnv, lEntry); ++lEntry; } mStore.mLength = 0; }