mariadb/client/completion_hash.cc

226 lines
4.5 KiB
C++
Raw Normal View History

/* Copyright (C) 2000 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
2000-07-31 21:29:14 +02:00
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
2000-07-31 21:29:14 +02:00
/* Quick & light hash implementation for tab completion purposes
*
* by Andi Gutmans <andi@zend.com>
* and Zeev Suraski <zeev@zend.com>
* Small portability changes by Monty. Changed also to use my_malloc/my_free
*/
#include <my_global.h>
2000-07-31 21:29:14 +02:00
#include <m_string.h>
#include <my_sys.h>
#include "completion_hash.h"
2002-11-19 15:26:53 +01:00
uint hashpjw(const char *arKey, uint nKeyLength)
2000-07-31 21:29:14 +02:00
{
uint h = 0, g, i;
for (i = 0; i < nKeyLength; i++) {
h = (h << 4) + arKey[i];
if ((g = (h & 0xF0000000))) {
h = h ^ (g >> 24);
h = h ^ g;
}
}
return h;
}
int completion_hash_init(HashTable *ht, uint nSize)
{
ht->arBuckets = (Bucket **) my_malloc(nSize* sizeof(Bucket *),
MYF(MY_ZEROFILL | MY_WME));
if (!ht->arBuckets)
{
2000-07-31 21:29:14 +02:00
ht->initialized = 0;
return FAILURE;
}
init_alloc_root(&ht->mem_root, 8192, 0);
2000-07-31 21:29:14 +02:00
ht->pHashFunction = hashpjw;
ht->nTableSize = nSize;
ht->initialized = 1;
return SUCCESS;
}
int completion_hash_update(HashTable *ht, char *arKey, uint nKeyLength,
char *str)
{
uint h, nIndex;
Bucket *p;
h = ht->pHashFunction(arKey, nKeyLength);
nIndex = h % ht->nTableSize;
if (nKeyLength <= 0) {
return FAILURE;
}
p = ht->arBuckets[nIndex];
while (p)
{
if ((p->h == h) && (p->nKeyLength == nKeyLength)) {
if (!memcmp(p->arKey, arKey, nKeyLength)) {
entry *n;
if (!(n = (entry *) alloc_root(&ht->mem_root,sizeof(entry))))
return FAILURE;
2000-07-31 21:29:14 +02:00
n->pNext = p->pData;
n->str = str;
p->pData = n;
p->count++;
return SUCCESS;
}
}
p = p->pNext;
}
if (!(p = (Bucket *) alloc_root(&ht->mem_root, sizeof(Bucket))))
2000-07-31 21:29:14 +02:00
return FAILURE;
2000-07-31 21:29:14 +02:00
p->arKey = arKey;
p->nKeyLength = nKeyLength;
p->h = h;
if (!(p->pData = (entry*) alloc_root(&ht->mem_root, sizeof(entry))))
2000-07-31 21:29:14 +02:00
return FAILURE;
2000-07-31 21:29:14 +02:00
p->pData->str = str;
p->pData->pNext = 0;
p->count = 1;
p->pNext = ht->arBuckets[nIndex];
ht->arBuckets[nIndex] = p;
return SUCCESS;
}
2002-11-19 15:26:53 +01:00
static Bucket *completion_hash_find(HashTable *ht, const char *arKey,
2000-07-31 21:29:14 +02:00
uint nKeyLength)
{
uint h, nIndex;
Bucket *p;
h = ht->pHashFunction(arKey, nKeyLength);
nIndex = h % ht->nTableSize;
p = ht->arBuckets[nIndex];
while (p)
{
if ((p->h == h) && (p->nKeyLength == nKeyLength)) {
if (!memcmp(p->arKey, arKey, nKeyLength)) {
return p;
}
}
p = p->pNext;
}
return (Bucket*) 0;
}
int completion_hash_exists(HashTable *ht, char *arKey, uint nKeyLength)
{
uint h, nIndex;
Bucket *p;
h = ht->pHashFunction(arKey, nKeyLength);
nIndex = h % ht->nTableSize;
p = ht->arBuckets[nIndex];
while (p)
{
if ((p->h == h) && (p->nKeyLength == nKeyLength))
{
if (!strcmp(p->arKey, arKey)) {
return 1;
}
}
p = p->pNext;
}
return 0;
}
2002-11-19 15:26:53 +01:00
Bucket *find_all_matches(HashTable *ht, const char *str, uint length,
2000-07-31 21:29:14 +02:00
uint *res_length)
{
Bucket *b;
b = completion_hash_find(ht,str,length);
if (!b) {
*res_length = 0;
return (Bucket*) 0;
} else {
*res_length = length;
return b;
}
}
Bucket *find_longest_match(HashTable *ht, char *str, uint length,
uint *res_length)
{
Bucket *b,*return_b;
char *s;
uint count;
uint lm;
b = completion_hash_find(ht,str,length);
if (!b) {
*res_length = 0;
return (Bucket*) 0;
}
count = b->count;
lm = length;
s = b->pData->str;
return_b = b;
while (s[lm]!=0 && (b=completion_hash_find(ht,s,lm+1))) {
if (b->count<count) {
*res_length=lm;
return return_b;
}
return_b=b;
lm++;
}
*res_length=lm;
return return_b;
}
void completion_hash_clean(HashTable *ht)
{
free_root(&ht->mem_root,MYF(0));
2000-07-31 21:29:14 +02:00
bzero((char*) ht->arBuckets,ht->nTableSize*sizeof(Bucket *));
}
void completion_hash_free(HashTable *ht)
{
completion_hash_clean(ht);
Bug#34043: Server loops excessively in _checkchunk() when safemalloc is enabled Essentially, the problem is that safemalloc is excruciatingly slow as it checks all allocated blocks for overrun at each memory management primitive, yielding a almost exponential slowdown for the memory management functions (malloc, realloc, free). The overrun check basically consists of verifying some bytes of a block for certain magic keys, which catches some simple forms of overrun. Another minor problem is violation of aliasing rules and that its own internal list of blocks is prone to corruption. Another issue with safemalloc is rather the maintenance cost as the tool has a significant impact on the server code. Given the magnitude of memory debuggers available nowadays, especially those that are provided with the platform malloc implementation, maintenance of a in-house and largely obsolete memory debugger becomes a burden that is not worth the effort due to its slowness and lack of support for detecting more common forms of heap corruption. Since there are third-party tools that can provide the same functionality at a lower or comparable performance cost, the solution is to simply remove safemalloc. Third-party tools can provide the same functionality at a lower or comparable performance cost. The removal of safemalloc also allows a simplification of the malloc wrappers, removing quite a bit of kludge: redefinition of my_malloc, my_free and the removal of the unused second argument of my_free. Since free() always check whether the supplied pointer is null, redudant checks are also removed. Also, this patch adds unit testing for my_malloc and moves my_realloc implementation into the same file as the other memory allocation primitives.
2010-07-08 23:20:08 +02:00
my_free(ht->arBuckets);
2000-07-31 21:29:14 +02:00
}
void add_word(HashTable *ht,char *str)
{
int i;
char *pos=str;
for (i=1; *pos; i++, pos++)
2000-07-31 21:29:14 +02:00
completion_hash_update(ht, str, i, str);
}