more efficient extension abi

This commit is contained in:
Akash Mozumdar 2019-02-18 20:57:10 -05:00
parent 8016ac178a
commit 29f616ae9d
2 changed files with 14 additions and 22 deletions

View File

@ -53,21 +53,14 @@ namespace
bool DispatchSentenceToExtensions(std::wstring& sentence, const InfoForExtension* miscInfo) bool DispatchSentenceToExtensions(std::wstring& sentence, const InfoForExtension* miscInfo)
{ {
wchar_t* sentenceBuffer = (wchar_t*)HeapAlloc(GetProcessHeap(), 0, (sentence.size() + 1) * sizeof(wchar_t)); wchar_t* sentenceBuffer = (wchar_t*)HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, (sentence.size() + 1) * sizeof(wchar_t));
wcscpy_s(sentenceBuffer, sentence.size() + 1, sentence.c_str()); wcscpy_s(sentenceBuffer, sentence.size() + 1, sentence.c_str());
concurrency::reader_writer_lock::scoped_lock_read readLock(extenMutex); concurrency::reader_writer_lock::scoped_lock_read readLock(extenMutex);
for (const auto& extension : extensions) for (const auto& extension : extensions)
{ if (*(sentenceBuffer = extension.callback(sentenceBuffer, miscInfo)) == L'\0') break;
wchar_t* nextBuffer = extension.callback(sentenceBuffer, miscInfo);
if (nextBuffer != sentenceBuffer) HeapFree(GetProcessHeap(), 0, sentenceBuffer);
if (nextBuffer == nullptr) return false;
sentenceBuffer = nextBuffer;
}
sentence = sentenceBuffer; sentence = sentenceBuffer;
HeapFree(GetProcessHeap(), 0, sentenceBuffer); HeapFree(GetProcessHeap(), 0, sentenceBuffer);
return true; return !sentence.empty();
} }
ExtenWindow::ExtenWindow(QWidget* parent) : ExtenWindow::ExtenWindow(QWidget* parent) :

View File

@ -5,12 +5,11 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo);
/** /**
* You shouldn't mess with this or even look at it unless you're certain you know what you're doing. * You shouldn't mess with this or even look at it unless you're certain you know what you're doing.
* Param sentence: pointer to sentence received by Textractor (UTF-16). * Param sentence: pointer to sentence received by Textractor (UTF-16).
* You should not write beyond the end of this sentence. If you want Textractor to receive a larger sentence, copy it into your own buffer and return that. * This can be modified. Textractor uses the modified sentence for future processing and display. If empty (starts with null terminator), Textractor will destroy it.
* Please allocate the buffer using HeapAlloc() and not new[] or malloc() or something else: Textractor uses HeapFree() to free it.
* Param miscInfo: pointer to array containing misc info about the sentence. End of array is marked with name being nullptr.
* Return value: pointer to sentence Textractor takes for future processing and display. If nullptr, Textractor will destroy the sentence.
* Return 'sentence' unless you created a new sentence/buffer as mentioned above.
* Textractor will display the sentence after all extensions have had a chance to process and/or modify it. * Textractor will display the sentence after all extensions have had a chance to process and/or modify it.
* The buffer is allocated using HeapAlloc(). If you want to make it larger, please use HeapReAlloc().
* Param miscInfo: pointer to array containing misc info about the sentence. End of array is marked with name being nullptr.
* Return value: the buffer used for the sentence. Remember to return a new pointer if HeapReAlloc() gave you one.
* THIS FUNCTION MAY BE RUN SEVERAL TIMES CONCURRENTLY: PLEASE ENSURE THAT IT IS THREAD SAFE! * THIS FUNCTION MAY BE RUN SEVERAL TIMES CONCURRENTLY: PLEASE ENSURE THAT IT IS THREAD SAFE!
*/ */
extern "C" __declspec(dllexport) wchar_t* OnNewSentence(wchar_t* sentence, const InfoForExtension* miscInfo) extern "C" __declspec(dllexport) wchar_t* OnNewSentence(wchar_t* sentence, const InfoForExtension* miscInfo)
@ -21,13 +20,13 @@ extern "C" __declspec(dllexport) wchar_t* OnNewSentence(wchar_t* sentence, const
int origLength = sentenceStr.size(); int origLength = sentenceStr.size();
if (ProcessSentence(sentenceStr, SentenceInfo{ miscInfo })) if (ProcessSentence(sentenceStr, SentenceInfo{ miscInfo }))
{ {
if (sentenceStr.empty()) return nullptr; if (sentenceStr.size() > origLength) sentence = (wchar_t*)HeapReAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, sentence, (sentenceStr.size() + 1) * sizeof(wchar_t));
// No need to worry about freeing this: Textractor does it for you. wcscpy_s(sentence, sentenceStr.size() + 1, sentenceStr.c_str());
wchar_t* newSentence = sentenceStr.size() > origLength ? (wchar_t*)HeapAlloc(GetProcessHeap(), 0, (sentenceStr.size() + 1) * sizeof(wchar_t)) : sentence;
wcscpy_s(newSentence, sentenceStr.size() + 1, sentenceStr.c_str());
return newSentence;
} }
else return sentence;
} }
catch (SKIP) { return nullptr; } catch (SKIP)
{
*sentence = L'\0';
}
return sentence;
} }