1 #pragma ident "%Z%%M% %I% %E% SMI"
2
3 /*
4 * This file is part of libdyn.a, the C Dynamic Object library. It
5 * contains the source code for the function DynDelete().
6 *
7 * There are no restrictions on this code; however, if you make any
8 * changes, I request that you document them so that I do not get
9 * credit or blame for your modifications.
10 *
11 * Written by Barr3y Jaspan, Student Information Processing Board (SIPB)
12 * and MIT-Project Athena, 1989.
13 * Copyright (c) 2016 by Delphix. All rights reserved.
14 */
15
16 #include <stdio.h>
17 #include <strings.h>
18 #include <string.h>
19
20 #include "dynP.h"
21
22 /*
23 * Checkers! Get away from that "hard disk erase" button!
24 * (Stupid dog. He almost did it to me again ...)
25 */
DynDelete(obj,idx)26 int DynDelete(obj, idx)
27 DynObjectP obj;
28 int idx;
29 {
30 if (idx < 0) {
31 if (obj->debug)
32 fprintf(stderr, "dyn: delete: bad index %d\n", idx);
33 return DYN_BADINDEX;
34 }
35
36 if (idx >= obj->num_el) {
37 if (obj->debug)
38 fprintf(stderr, "dyn: delete: Highest index is %d.\n",
39 obj->num_el);
40 return DYN_BADINDEX;
41 }
42
43 if (idx == obj->num_el-1) {
44 if (obj->paranoid) {
45 if (obj->debug)
46 fprintf(stderr, "dyn: delete: last element, zeroing.\n");
47 memset(obj->array + idx*obj->el_size, 0, obj->el_size);
48 }
49 else {
50 if (obj->debug)
51 fprintf(stderr, "dyn: delete: last element, punting.\n");
52 }
53 }
54 else {
55 if (obj->debug)
56 fprintf(stderr,
57 "dyn: delete: copying %d bytes from %d + %d to + %d.\n",
58 obj->el_size*(obj->num_el - idx), obj->array,
59 (idx+1)*obj->el_size, idx*obj->el_size);
60
61 #ifdef HAVE_MEMMOVE
62 memmove(obj->array + idx*obj->el_size,
63 obj->array + (idx+1)*obj->el_size,
64 obj->el_size*(obj->num_el - idx));
65 #else
66 bcopy(obj->array + (idx+1)*obj->el_size,
67 obj->array + idx*obj->el_size,
68 obj->el_size*(obj->num_el - idx));
69 #endif
70 if (obj->paranoid) {
71 if (obj->debug)
72 fprintf(stderr,
73 "dyn: delete: zeroing %d bytes from %d + %d\n",
74 obj->el_size, obj->array,
75 obj->el_size*(obj->num_el - 1));
76 memset(obj->array + obj->el_size*(obj->num_el - 1), 0,
77 obj->el_size);
78 }
79 }
80
81 --obj->num_el;
82
83 if (obj->debug)
84 fprintf(stderr, "dyn: delete: done.\n");
85
86 return DYN_OK;
87 }
88