-
Notifications
You must be signed in to change notification settings - Fork 1
/
c_vector_alloc.c
88 lines (78 loc) · 2.01 KB
/
c_vector_alloc.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* c_vector_alloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aaubin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/02/12 22:10:00 by aaubin #+# #+# */
/* Updated: 2014/02/15 05:59:39 by aleger ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "c_vector.h"
static void vector_bzero(void *s, size_t n)
{
char *t;
if (n)
{
t = s;
while (n)
{
*t++ = 0;
n--;
}
}
}
void *vector_memcpy(void *s1, const void *s2, size_t n)
{
char *dp;
const char *sp;
sp = s2;
dp = s1;
while (n--)
*dp++ = *sp++;
return (s1);
}
static void vector_memdel(void **ap)
{
if (ap != NULL)
{
free(*ap);
*ap = NULL;
}
}
void *vector_memalloc(size_t size)
{
void *memory;
unsigned int counter;
char *explorer;
counter = 0;
memory = (void *)malloc(size);
explorer = (char *)memory;
if (memory != NULL)
{
while (counter++ < size)
*explorer++ = 0;
}
return (memory);
}
void *vector_realloc(void *ptr, size_t old, size_t size)
{
void *new;
if (ptr == NULL)
return (NULL);
if (size == 0)
new = (void *)vector_memalloc(sizeof(char));
else
new = (void *)vector_memalloc(size);
if (new == NULL)
return (NULL);
vector_bzero(new, (size) ? size : sizeof(char));
if (old > size)
vector_memcpy(new, ptr, (size) ? size : sizeof(char));
else
vector_memcpy(new, ptr, old);
vector_memdel((void *)&ptr);
return (new);
}