Added paranoid check to UnregisterDataSource() in the case that the datasource was never registered & tries to unregister in its destructor. Optimization to GetResource() -- do away with nsString's as they were causing a lot of heap activity.

This commit is contained in:
waterson%netscape.com 1999-03-27 00:29:35 +00:00
Родитель e226dd45e4
Коммит 089b59eec1
1 изменённых файлов: 20 добавлений и 15 удалений

Просмотреть файл

@ -516,9 +516,8 @@ ServiceImpl::GetResource(const char* aURI, nsIRDFResource** aResource)
// Nope. So go to the repository to create it. // Nope. So go to the repository to create it.
nsresult rv; nsresult rv;
nsAutoString uriStr(aURI); char* p = PL_strchr(aURI, ':');
PRInt32 pos = uriStr.Find(':'); if (!p) {
if (pos < 0) {
// no colon, so try the default resource factory // no colon, so try the default resource factory
rv = nsComponentManager::CreateInstance(NS_RDF_RESOURCE_FACTORY_PROGID, rv = nsComponentManager::CreateInstance(NS_RDF_RESOURCE_FACTORY_PROGID,
nsnull, nsIRDFResource::GetIID(), nsnull, nsIRDFResource::GetIID(),
@ -532,20 +531,24 @@ ServiceImpl::GetResource(const char* aURI, nsIRDFResource** aResource)
else { else {
// the resource is qualified, so construct a ProgID and // the resource is qualified, so construct a ProgID and
// construct it from the repository. // construct it from the repository.
nsAutoString progIDStr; static const char kRDFResourceFactoryProgIDPrefix[]
uriStr.Left(progIDStr, pos); // truncate = NS_RDF_RESOURCE_FACTORY_PROGID_PREFIX;
progIDStr.Insert(NS_RDF_RESOURCE_FACTORY_PROGID_PREFIX, 0);
PRInt32 pos = (p) ? (p - aURI) : (-1);
PRInt32 len = pos + sizeof(kRDFResourceFactoryProgIDPrefix) - 1;
// Safely convert to a C-string for the XPCOM APIs // Safely convert to a C-string for the XPCOM APIs
char buf[128]; char buf[128];
char* progID = buf; char* progID = buf;
if (progIDStr.Length() >= sizeof(buf)) if (len >= sizeof(buf))
progID = new char[progIDStr.Length() + 1]; progID = new char[len + 1];
if (progID == nsnull) if (progID == nsnull)
return NS_ERROR_OUT_OF_MEMORY; return NS_ERROR_OUT_OF_MEMORY;
progIDStr.ToCString(progID, progIDStr.Length() + 1); PL_strcpy(progID, kRDFResourceFactoryProgIDPrefix);
PL_strncpy(progID + sizeof(kRDFResourceFactoryProgIDPrefix) - 1, aURI, pos);
progID[len] = '\0';
rv = nsComponentManager::CreateInstance(progID, nsnull, rv = nsComponentManager::CreateInstance(progID, nsnull,
nsIRDFResource::GetIID(), nsIRDFResource::GetIID(),
@ -597,7 +600,7 @@ ServiceImpl::FindResource(const char* uri, nsIRDFResource** resource, PRBool *fo
NS_IMETHODIMP NS_IMETHODIMP
ServiceImpl::GetUnicodeResource(const PRUnichar* aURI, nsIRDFResource** aResource) ServiceImpl::GetUnicodeResource(const PRUnichar* aURI, nsIRDFResource** aResource)
{ {
nsString uriStr(aURI); nsAutoString uriStr(aURI);
char buf[128]; char buf[128];
char* uri = buf; char* uri = buf;
@ -773,13 +776,15 @@ ServiceImpl::UnregisterDataSource(nsIRDFDataSource* aDataSource)
if (NS_FAILED(rv = aDataSource->GetURI(&uri))) if (NS_FAILED(rv = aDataSource->GetURI(&uri)))
return rv; return rv;
nsIRDFDataSource* ds = if (uri) {
NS_STATIC_CAST(nsIRDFDataSource*, PL_HashTableLookup(mNamedDataSources, uri)); nsIRDFDataSource* ds =
NS_STATIC_CAST(nsIRDFDataSource*, PL_HashTableLookup(mNamedDataSources, uri));
if (! ds) if (! ds)
return NS_ERROR_ILLEGAL_VALUE; return NS_ERROR_ILLEGAL_VALUE;
PL_HashTableRemove(mNamedDataSources, uri); PL_HashTableRemove(mNamedDataSources, uri);
}
return NS_OK; return NS_OK;
} }