root/branches/windows-client/src/common/misc/pint-mem.c @ 8560

Revision 8560, 1.5 KB (checked in by sampson, 3 years ago)

Porting BMI TCP

Line 
1/*
2 * (C) 2001 Clemson University and The University of Chicago
3 *
4 * See COPYING in top-level directory.
5 */
6
7#define _XOPEN_SOURCE 600
8#include <errno.h>
9#include <stdlib.h>
10#include <string.h>
11#ifdef HAVE_MALLOC_H
12#include <malloc.h>
13#endif
14
15#ifdef WIN32
16#include "wincommon.h"
17
18/* do not declare inline on Windows (can't be exported)*/
19#undef inline
20#define inline
21#endif
22
23/* prototype definitions */
24inline void* PINT_mem_aligned_alloc(size_t size, size_t alignment);
25inline void PINT_mem_aligned_free(void *ptr);
26
27/* PINT_mem_aligned_alloc()
28 *
29 * allocates a memory region of the specified size and returns a
30 * pointer to the region.  The address of the memory will be evenly
31 * divisible by alignment.
32 *
33 * returns pointer to memory on success, NULL on failure
34 */
35inline void* PINT_mem_aligned_alloc(size_t size, size_t alignment)
36{
37    int ret;
38    void *ptr;
39
40#ifdef WIN32   
41    ret = 0;
42    ptr = _aligned_malloc(size, alignment);
43    if (ptr == NULL)
44    {
45        ret = ENOMEM;
46    }
47#else
48    ret = posix_memalign(&ptr, alignment, size);
49#endif
50    if(ret != 0)
51    {
52        errno = ret;
53        return NULL;
54    }
55    memset(ptr, 0, size);
56    return ptr;
57}
58
59/* PINT_mem_aligned_free()
60 *
61 * frees memory region previously allocated with
62 * PINT_mem_aligned_alloc()
63 *
64 * no return value
65 */
66inline void PINT_mem_aligned_free(void *ptr)
67{
68    _aligned_free(ptr);
69    return;
70}
71
72/*
73 * Local variables:
74 *  c-indent-level: 4
75 *  c-basic-offset: 4
76 * End:
77 *
78 * vim: ts=8 sts=4 sw=4 expandtab
79 */
80
81
Note: See TracBrowser for help on using the browser.