aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/interceptor/kernel_alloc.c
blob: 21af9ba3d5c2d1523f939c4617b6c8c14dcbdd01 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* Netfilter Driver for IPSec VPN Client
 *
 * Copyright(c)   2012 Samsung Electronics
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

/*
 * kernel_alloc.
 *
 * Engine memory allocation API implementation for kernel space.
 *
 */

#include "sshincludes.h"
#include "kernel_alloc.h"
#include "kernel_mutex.h"

void *
ssh_malloc_flags(size_t size, SshUInt32 flags)
{
  return ssh_kernel_alloc(size, flags);
}

void *
ssh_malloc(size_t size)
{
  return ssh_malloc_flags(size, SSH_KERNEL_ALLOC_NOWAIT);
}

void *
ssh_realloc_flags(void *oldptr, size_t oldsize, size_t newsize,
                  SshUInt32 flags)
{
  void * newptr;

  if (oldptr == NULL)
    return ssh_kernel_alloc(newsize, flags);

  if (newsize <= oldsize)
    return oldptr;

  if ((newptr = ssh_kernel_alloc(newsize, flags)) == NULL)
      return NULL;

  /* newsize > oldsize, see above */
  if (oldsize > 0)
    memcpy(newptr, oldptr, oldsize);

  /* Success, thus we can release the old memory */
  ssh_kernel_free(oldptr);

  return newptr;
}

void *
ssh_realloc(void * oldptr, size_t oldsize, size_t newsize)
{
  return ssh_realloc_flags(oldptr, oldsize, newsize, SSH_KERNEL_ALLOC_NOWAIT);
}

/* coverity[ -tainted_data_sink : arg-0 ] */
void ssh_free (void * ptr)
{
  if (ptr != NULL)
    ssh_kernel_free(ptr);
}

void*
ssh_calloc_flags (size_t nitems, size_t isize, SshUInt32 flags)
{
  void                * ptr;
  unsigned long       size;

  size = isize * nitems;

  if ((ptr = ssh_malloc_flags(size ? size : 1, flags)) == NULL)
    return NULL;

  if (size > 0)
    memset(ptr, 0, size);

  return ptr;
}

void *
ssh_calloc(size_t nitems, size_t isize)
{
  return ssh_calloc_flags(nitems, isize, SSH_KERNEL_ALLOC_NOWAIT);
}

void *ssh_strdup (const void * p)
{
  const char  * str;
  char        * cp;

  SSH_PRECOND(p != NULL);

  str = (const char *) p;

  if ((cp = (char *) ssh_malloc(strlen(str) + 1)) == NULL)
    return NULL;

  strcpy(cp, str);

  return (void *) cp;
}

void *ssh_memdup(const void * p, size_t len)
{
  void        * cp;

  if ((cp = ssh_malloc(len + 1)) == NULL)
    return NULL;

  memcpy(cp, p, (size_t)len);

  ((unsigned char *) cp)[len] = '\0';

  return cp;
}