~mingw-w64/mingw-w64/experimental

« back to all changes in this revision

Viewing changes to libw64crt/memory/aligned_offset_realloc.c

  • Committer: ktietz70
  • Date: 2009-12-07 19:49:25 UTC
  • Revision ID: svn-v4:4407c894-4637-0410-b4f5-ada5f102cad1:experimental:1611
Rename to iCrt (ironCrate)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <w64crt.h>
2
 
#include "aligned.h"
3
 
 
4
 
void *
5
 
__w64crt_aligned_offset_realloc (void *ptr, size_t sz, size_t align, size_t off)
6
 
{
7
 
  void *tmp, **savep;
8
 
  size_t pad_old, pad_new, sz_old;
9
 
 
10
 
  if (!ptr)
11
 
    return __w64crt_aligned_offset_malloc (sz, align, off);
12
 
 
13
 
  if ((align & (align - 1)) != 0 || off >= sz)
14
 
    {
15
 
      __w64crt_set_errno(EINVAL);
16
 
      return NULL;
17
 
    }
18
 
 
19
 
  if (!sz)
20
 
    {
21
 
      __w64crt_aligned_free (ptr);
22
 
        return NULL;
23
 
    }
24
 
 
25
 
  if (align < sizeof (void *))
26
 
    align = sizeof (void *);
27
 
 
28
 
  savep = (void **) SAVED_PTR (ptr);
29
 
  if (ptr != ALIGN_PTR(*savep, align, off))
30
 
    {
31
 
      __w64crt_set_errno (EINVAL);
32
 
      return NULL;
33
 
    }
34
 
 
35
 
  pad_old = (char *) ptr - (char *) *savep;
36
 
  sz_old = _msize (*savep);
37
 
  if (sz_old == SZ_MINUSONE || sz_old < pad_old)
38
 
    {
39
 
      __w64crt_set_errno (EINVAL);
40
 
      return NULL;
41
 
    }
42
 
 
43
 
  sz_old -= pad_old;
44
 
 
45
 
  tmp = __w64crt_realloc (*savep, sz + align + sizeof (void *));
46
 
 
47
 
  if (!tmp)
48
 
    return NULL;
49
 
 
50
 
  ptr = ALIGN_PTR (tmp, align, off);
51
 
  savep = (void **) SAVED_PTR (ptr);
52
 
  pad_new = (char *) ptr - (char *) tmp;
53
 
 
54
 
  if (pad_new != pad_old)
55
 
    memmove ((char *) ptr, (char *) tmp + pad_old, (sz_old < sz ? sz_old : sz));
56
 
 
57
 
  *savep = tmp;
58
 
  return ptr;
59
 
}