}
/* Map allocation to a size class. */
- if (size <= lengthof(sb_size_class_map) * SB_SIZE_CLASS_MAP_QUANTUM)
+ if (size < lengthof(sb_size_class_map) * SB_SIZE_CLASS_MAP_QUANTUM)
{
int mapidx;
LWLockRelease(lock);
}
+/*
+ * Return the size of the chunk that will be used to satisfy a given
+ * allocation.
+ */
+Size
+sb_alloc_space(Size size)
+{
+ uint16 size_class;
+
+ /* Large objects allocate full pages. */
+ if (size > sb_size_classes[lengthof(sb_size_classes) - 1])
+ return FPM_PAGE_SIZE * fpm_size_to_pages(size);
+
+ /* Map request size to a size class. */
+ if (size < lengthof(sb_size_class_map) * SB_SIZE_CLASS_MAP_QUANTUM)
+ {
+ int mapidx;
+
+ mapidx = ((size + SB_SIZE_CLASS_MAP_QUANTUM - 1) /
+ SB_SIZE_CLASS_MAP_QUANTUM) - 1;
+ size_class = sb_size_class_map[mapidx];
+ }
+ else
+ {
+ uint16 min = sb_size_class_map[lengthof(sb_size_class_map) - 1];
+ uint16 max = lengthof(sb_size_classes) - 1;
+ while (min < max)
+ {
+ uint16 mid = (min + max) / 2;
+ uint16 class_size = sb_size_classes[mid];
+
+ if (class_size < size)
+ min = mid + 1;
+ else
+ max = mid;
+ }
+ size_class = min;
+ }
+
+ return sb_size_classes[size_class];
+}
+
/*
* Return the size of the chunk used to satisfy a given allocation.
*
/* copy the tuple into sort storage */
newtuple = (IndexTuple) sb_alloc(state->sortallocator, tuplen, 0);
memcpy(newtuple, tuple, tuplen);
- USEMEM(state, sb_chunk_space(newtuple));
+ USEMEM(state, sb_alloc_space(tuplen));
stup->tuple = (void *) newtuple;
/* set up first-column key value */
stup->datum1 = index_getattr(newtuple,
#define SB_ALLOC_HUGE 0x0001 /* allow >=1GB */
#define SB_ALLOC_SOFT_FAIL 0x0002 /* return NULL if no mem */
-/* Exported functions. */
+/* Functions to manipulate allocators. */
extern sb_allocator *sb_create_private_allocator(void);
+extern void sb_reset_allocator(sb_allocator *a);
+extern void sb_destroy_private_allocator(sb_allocator *);
+
+/* Functions to allocate and free memory. */
extern void *sb_alloc(sb_allocator *, Size, int flags);
extern void sb_free(void *ptr);
+
+/* Reporting functions. */
+extern Size sb_alloc_space(Size size);
extern Size sb_chunk_space(void *ptr);
-extern void sb_reset_allocator(sb_allocator *a);
-extern void sb_destroy_private_allocator(sb_allocator *);
#endif /* SB_ALLOC_H */