BaseCache::Slab * BaseCache::ConstructSlab(Slab *slab, void *pages, size_t byteCount, ObjectLink *(*getLink)(void *parent, void *object), void *parent) { printf("BaseCache::ConstructSlab(%p, %p, %lu, %p, %p)\n", slab, pages, byteCount, getLink, parent); slab->pages = pages; slab->count = slab->size = byteCount / fObjectSize; slab->free = NULL; size_t spareBytes = byteCount - (slab->size * fObjectSize); size_t cycle = fCacheColorCycle; if (cycle > spareBytes) cycle = 0; else fCacheColorCycle += kCacheColorPeriod; printf(" %lu objects, %lu spare bytes, cycle %lu\n", slab->size, spareBytes, cycle); uint8_t *data = ((uint8_t *)pages) + cycle; for (size_t i = 0; i < slab->size; i++) { if (fConstructor) fConstructor(fCookie, data); SListPush(slab->free, getLink(parent, data)); data += fObjectSize; } return slab; }
bool BaseDepot::_ExchangeWithFull(Magazine* &magazine) { BenaphoreLocker _(fLock); if (fFull == NULL) return false; fFullCount--; fEmptyCount++; SListPush(fEmpty, magazine); magazine = SListPop(fFull); return true; }
bool BaseCache::ReturnObject(const ObjectInfo &object) { Slab *slab = object.first; ObjectLink *link = object.second; // We return true if the slab is completely unused. SListPush(slab->free, link); slab->count++; if (slab->count == slab->size) { fPartialSlabs.Remove(slab); return true; } else if (slab->count == 1) { fFullSlabs.Remove(slab); fPartialSlabs.Add(slab); } return false; }
bool BaseDepot::_ExchangeWithEmpty(Magazine* &magazine) { BenaphoreLocker _(fLock); if (fEmpty == NULL) { fEmpty = _AllocMagazine(); if (fEmpty == NULL) return false; } else { fEmptyCount--; } if (magazine) { SListPush(fFull, magazine); fFullCount++; } magazine = SListPop(fEmpty); return true; }
S_LOCAL void _s_load_voice_utterance_processors(const SMap *voiceConfig, SVoice *voice, s_erc *error) { SMap *uttProcessors; const SObject *tmp; const SMap *voiceConfigUttProcessors; s_bool key_present; SIterator *itr; const char *uttproc_name; const char *uttproc_class; const char *uttproc_plugin; const SMap *uttProcInfo; const SMap *uttProcFeats; SUttProcessor *uProcessor; SPlugin *plugin; SList *uttProcPlugins; S_CLR_ERR(error); /* create a map container for utterance-processors */ uttProcessors = S_MAP(S_NEW(SMapList, error)); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Failed to create new map for voice utterance-processors")) return; /* look for "utterance-processors" key in voiceConfig map */ key_present = SMapObjectPresent(voiceConfig, "utterance-processors", error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"SMapObjectPresent\" failed for \'utterance-processors\' key")) { S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); return; } if (!key_present) { /* no defined utterance-processors */ voice->uttProcessors = uttProcessors; return; } /* get utterance-processors from voiceConfig */ tmp = SMapGetObject(voiceConfig, "utterance-processors", error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"SMapGetObject\" failed")) { S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); return; } voiceConfigUttProcessors = S_CAST(tmp, SMap, error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"S_CAST (SMap)\" failed for \'utterance-processors\' object")) { S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); return; } /* * iterate through the voiceConfigUttProcessors, create the * utterance-processors and add their features (if any), and add * them to the uttProcessors map */ itr = S_ITERATOR_GET(voiceConfigUttProcessors, error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"S_ITERATOR_GET\" failed")) { S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); return; } if (itr) { /* create a plug-in list */ uttProcPlugins = S_LIST(S_NEW(SListList, error)); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Failed to create new list for voice utterance-processor plug-ins")) { S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); return; } } else uttProcPlugins = NULL; while (itr) { /* the utterance-processor name */ uttproc_name = SIteratorKey(itr, error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"SIteratorKey\" failed")) { S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } /* the utterance-processor info (SMap) */ tmp = SIteratorObject(itr, error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"SIteratorObject\" failed")) { S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } uttProcInfo = S_CAST(tmp, SMap, error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"S_CAST (SMap)\" failed for utterance-processor \'%s\'", uttproc_name)) { S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } /* * uttProcInfo must have "class" and "plug-in" and * optionally "features" */ uttproc_class = SMapGetString(uttProcInfo, "class", error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"SMapGetString\" failed for \'class\' name of utterance-processor \'%s\'", uttproc_name)) { S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } uttproc_plugin = SMapGetString(uttProcInfo, "plug-in", error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"SMapGetString\" failed for \'plug-in\' name of utterance-processor \'%s\'", uttproc_name)) { S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } /* * load the plug-in, add it to the list of utterance-processor plug-ins and create * the utterance-processor object */ plugin = s_pm_load_plugin(uttproc_plugin, error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"s_pm_load_plugin\" failed for utterance-processor \'%s\'", uttproc_name)) { S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } SListPush(uttProcPlugins, S_OBJECT(plugin), error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"SListPush\" failed for utterance-processor plug-ins")) { S_DELETE(plugin, "_s_load_voice_utterance_processors", error); S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } uProcessor = S_UTTPROCESSOR(S_NEW_FROM_NAME(uttproc_class, error)); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Failed to create new utterance-processor of class \'%s\'", uttproc_class)) { S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } /* add the utterance-processor to the map of utterance-processors */ SMapSetObject(uttProcessors, uttproc_name, S_OBJECT(uProcessor), error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"SMapSetObject\" failed")) { S_DELETE(uProcessor, "_s_load_voice_utterance_processors", error); S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } /* Check if this utterance-processor has any defined features */ key_present = SMapObjectPresent(uttProcInfo, "features", error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"SMapObjectPresent\" failed for \'features\' key of utterance-processor \'%s\'", uttproc_name)) { S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } if (key_present) { /* get utterance-processors "features" */ tmp = SMapGetObject(uttProcInfo, "features", error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"SMapGetObject\" failed for \'features\' of utterance-processor \'%s\'", uttproc_name)) { S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } uttProcFeats = S_CAST(tmp, SMap, error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"S_CAST (SMap)\" failed for \'features\' of utterance-processor \'%s\'", uttproc_name)) { S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } /* copy them to the utterance-processor */ SMapCopy(uProcessor->features, uttProcFeats, error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"SMapCopy\" failed for \'features\' of utterance-processor \'%s\'", uttproc_name)) { S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } } /* initialize the utterance processor */ SUttProcessorInit(&uProcessor, voice, error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"SUttProcessorInit\" failed for utterance-processor of class \'%s\'", uttproc_class)) { S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } itr = SIteratorNext(itr); } if (uttProcPlugins != NULL) { SListMerge(voice->plugins, uttProcPlugins, error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"SListMerge\" failed")) { S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); } voice->uttProcessors = uttProcessors; }
int main() { s_erc error = S_SUCCESS; SList *list = NULL; SIterator *itr; SObject *a = NULL; SObject *b = NULL; SObject *c = NULL; PyObject *speectModule = NULL; /* for SWIG functions to work */ /* Initialize the Python interpreter. Required. */ Py_Initialize(); /* import speect python module */ speectModule = PyImport_ImportModule("speect"); if (speectModule == NULL) { char *py_error = s_get_python_error_str(); if (py_error) { S_CTX_ERR(&error, S_FAILURE, "main", "Call to \"PyImport_ImportModule\" failed. Reported error: %s", py_error); S_FREE(py_error); } else { S_CTX_ERR(&error, S_FAILURE, "main", "Call to \"PyImport_ImportModule\" failed"); } goto quit; } /* * initialize speect python native module */ error = s_python_native_objects_init(); if (error != S_SUCCESS) { printf("Failed to initialize Speect Python native module\n"); goto quit; } /* Create and populate Python list */ list = create_and_populate_py_list(&error); if (S_CHK_ERR(&error, S_CONTERR, "main", "Call to \"create_and_populate_py_list\" failed")) goto quit; /* * get iterator to list, should be NULL as there are no objects * in the list */ itr = S_ITERATOR_GET(list, &error); if (S_CHK_ERR(&error, S_CONTERR, "main", "Failed to get iterator to list")) goto quit; /* Create some objects and put the into the list */ a = SObjectSetInt(10, &error); if (S_CHK_ERR(&error, S_CONTERR, "main", "Failed to set int object")) goto quit; b = SObjectSetFloat(3.14, &error); if (S_CHK_ERR(&error, S_CONTERR, "main", "Failed to set float object")) goto quit; c = SObjectSetString("python list test", &error); if (S_CHK_ERR(&error, S_CONTERR, "main", "Failed to set string object")) goto quit; SListPush(list, a, &error); if (S_CHK_ERR(&error, S_CONTERR, "main", "Failed to add object to list")) goto quit; else a = NULL; /* object belongs to list now, we don't want to * delete it directly. */ SListPush(list, b, &error); if (S_CHK_ERR(&error, S_CONTERR, "main", "Failed to add object to list")) goto quit; else b = NULL; /* object belongs to list now, we don't want to * delete it directly. */ SListPush(list, c, &error); if (S_CHK_ERR(&error, S_CONTERR, "main", "Failed to add object to list")) goto quit; else c = NULL; /* object belongs to list now, we don't want to * delete it directly. */ /* * get iterator to list, should not be NULL as there are now * objects in the list */ itr = S_ITERATOR_GET(list, &error); if (S_CHK_ERR(&error, S_CONTERR, "main", "Failed to get iterator to list")) goto quit; /* iterate through list objects and print them to stdout */ for (/* NOP */; itr != NULL; itr = SIteratorNext(itr)) { char *buf; const SObject *tmp; tmp = SIteratorObject(itr, &error); if (S_CHK_ERR(&error, S_CONTERR, "main", "Failed to get list iterator object")) goto quit; buf = SObjectPrint(tmp, &error); if (S_CHK_ERR(&error, S_CONTERR, "main", "Failed to print object")) goto quit; printf("list object = %s\n", buf); S_FREE(buf); } quit: if (list != NULL) S_DELETE(list, "main", &error); if (itr != NULL) S_DELETE(itr, "main", &error); if (a != NULL) S_DELETE(a, "main", &error); if (b != NULL) S_DELETE(b, "main", &error); if (c != NULL) S_DELETE(c, "main", &error); Py_XDECREF(speectModule); Py_Finalize(); return 0; }