* Allocate 'size' bytes from the currently active HashMemoryChunk */
| 3422 | * Allocate 'size' bytes from the currently active HashMemoryChunk |
| 3423 | */ |
| 3424 | static void * |
| 3425 | dense_alloc(HashJoinTable hashtable, Size size) |
| 3426 | { |
| 3427 | HashMemoryChunk newChunk; |
| 3428 | char *ptr; |
| 3429 | |
| 3430 | /* just in case the size is not already aligned properly */ |
| 3431 | size = MAXALIGN(size); |
| 3432 | |
| 3433 | /* |
| 3434 | * If tuple size is larger than threshold, allocate a separate chunk. |
| 3435 | */ |
| 3436 | if (size > HASH_CHUNK_THRESHOLD) |
| 3437 | { |
| 3438 | /* allocate new chunk and put it at the beginning of the list */ |
| 3439 | newChunk = (HashMemoryChunk) MemoryContextAlloc(hashtable->batchCxt, |
| 3440 | HASH_CHUNK_HEADER_SIZE + size); |
| 3441 | newChunk->maxlen = size; |
| 3442 | newChunk->used = size; |
| 3443 | newChunk->ntuples = 1; |
| 3444 | |
| 3445 | /* |
| 3446 | * Add this chunk to the list after the first existing chunk, so that |
| 3447 | * we don't lose the remaining space in the "current" chunk. |
| 3448 | */ |
| 3449 | if (hashtable->chunks != NULL) |
| 3450 | { |
| 3451 | newChunk->next = hashtable->chunks->next; |
| 3452 | hashtable->chunks->next.unshared = newChunk; |
| 3453 | } |
| 3454 | else |
| 3455 | { |
| 3456 | newChunk->next.unshared = hashtable->chunks; |
| 3457 | hashtable->chunks = newChunk; |
| 3458 | } |
| 3459 | |
| 3460 | return HASH_CHUNK_DATA(newChunk); |
| 3461 | } |
| 3462 | |
| 3463 | /* |
| 3464 | * See if we have enough space for it in the current chunk (if any). If |
| 3465 | * not, allocate a fresh chunk. |
| 3466 | */ |
| 3467 | if ((hashtable->chunks == NULL) || |
| 3468 | (hashtable->chunks->maxlen - hashtable->chunks->used) < size) |
| 3469 | { |
| 3470 | /* allocate new chunk and put it at the beginning of the list */ |
| 3471 | newChunk = (HashMemoryChunk) MemoryContextAlloc(hashtable->batchCxt, |
| 3472 | HASH_CHUNK_HEADER_SIZE + HASH_CHUNK_SIZE); |
| 3473 | |
| 3474 | newChunk->maxlen = HASH_CHUNK_SIZE; |
| 3475 | newChunk->used = size; |
| 3476 | newChunk->ntuples = 1; |
| 3477 | |
| 3478 | newChunk->next.unshared = hashtable->chunks; |
| 3479 | hashtable->chunks = newChunk; |
| 3480 | |
| 3481 | return HASH_CHUNK_DATA(newChunk); |
no test coverage detected