summaryrefslogtreecommitdiffstats
path: root/allocsize.c
blob: d427c30ebdd2a34cf2c2f873caf26bb957d9673a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* ----------------------------------------------------------------------- *
 *   
 *   Copyright 2000-2008 H. Peter Anvin - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as
 *   published by the Free Software Foundation, Inc.,
 *   59 Temple Place Ste 330, Boston MA 02111-1307, USA, version 2.1,
 *   incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * allocsize.c
 *
 * Detemine the "true" size of the specified allocation
 */

#include <assert.h>
#include <stdlib.h>
#include <inttypes.h>
#include <limits.h>
#include <string.h>

#include "lpsm.h"
#include "internals.h"
#include "bitops.h"

static size_t
lpsm_alloc_size_buddy(const void *ptr)
{
  struct buddy_params bp;
  bp = lpsm_find_alloc_buddy(ptr);

  return ((size_t)1) << (AH->arena_size_lg2-bp.rorder);
}

static size_t
lpsm_alloc_size_slab(const void *ptr)
{
  const struct slab_header *sh;
  const struct slab_info   *si;

  sh = (struct slab_header *)((uintptr_t)ptr & ~(BUDDY_SIZE_MIN-1));
  si = &AH->slab[sh->index];

  return si->size;
}

size_t
lpsm_alloc_size(const void *ptr)
{
  if ( !ptr )
    return 0;
  
  /* Buddy allocations are ALWAYS page-aligned, SLAB allocations never */
  if ( (uintptr_t)ptr & (BUDDY_SIZE_MIN-1) )
    return lpsm_alloc_size_slab(ptr);
  else
    return lpsm_alloc_size_buddy(ptr);
}