+/EE312

software design

git clone https://git.julia.blue/+/EE312

Joseph RyanFix memory leak796bbdc

master
3.0 KiB124 linesraw
1// String ADT Project3.cpp
2// EE 312 Project 3 submission by
3// Joseph Ryan
4// jbr2558
5// Slip days used: 0
6// Spring 2018
7
8#include <assert.h>
9#include <stdbool.h>
10#include <stdlib.h>
11#include <stdint.h>
12#include <string.h>
13#include "String.h"
14
15#define SIGNATURE (~0xdeadbeef)
16
17/*
18 * Helper macro to find the signature of a UTString
19 * Treat this like a function that takes a UTString*
20 * Accesses a uint32_t from where the signature should be located
21 */
22#define CHECK(s) (*(uint32_t*) ((s)->string + (s)->length + 1))
23
24/*
25 * Checks if a UTString is valid or not.
26 */
27bool isOurs(UTString* s) {
28    if (CHECK(s) == SIGNATURE) {
29        return true;
30    } else {
31        return false;
32    }
33}
34
35UTString* addCheck(UTString* s) {
36    s->string[s->length] = '\0';
37    CHECK(s) = ~0xdeadbeef;
38    return s;
39}
40
41/*
42 * Allocate a utstring on the heap.
43 * Initialize the string correctly by copying src.
44 * Return a pointer to the UTString.
45 */
46UTString* utstrdup(const char* src) {
47    UTString* newstr = (UTString*)malloc(sizeof(UTString));
48    newstr->length = strlen(src);
49    newstr->capacity = newstr->length;
50    newstr->string = strdup(src);
51    addCheck(newstr);
52    return newstr;
53}
54
55/*
56 * Returns the length of s.
57 *  s must be a valid UTString.
58 */
59uint32_t utstrlen(const UTString* s) {
60    assert(isOurs((UTString*)s));
61    return s->length;
62}
63
64/*
65 * Append the string suffix to the UTString s.
66 *  s must be a valid UTString.
67 * Do not allocate any additional storage: only append as many characters
68 *  as will actually fit in s.
69 * Update the length of s.
70 * Return s with the above changes. */
71UTString* utstrcat(UTString* s, const char* suffix) {
72    assert(isOurs(s));
73    int32_t len = strlen(suffix);
74    for(int32_t i = 0; s->length < s->capacity && i < len; ++i, ++s->length) {
75        s->string[s->length] = suffix[i];
76    }
77    addCheck(s);
78    return s;
79}
80
81/*
82 * Copy src into dst.
83 *  dst must be a valid UTString.
84 *  src is an ordinary string.
85 * Do not allocate any additional storage: only copy as many characters
86 *  as will actually fit in dst.
87 * Update the length of dst.
88 * Return dst with the above changes.
89 */
90UTString* utstrcpy(UTString* dst, const char* src) {
91    assert(isOurs(dst));
92    dst->length = 0;
93    utstrcat(dst, src);
94    return dst;
95}
96
97/*
98 * Free all memory associated with the given UTString.
99 */
100void utstrfree(UTString* self) {
101    assert(isOurs(self));
102    free(self->string);
103    free(self);
104}
105
106/*
107 * Make s have at least as much capacity as new_capacity.
108 *  s must be a valid UTString.
109 * If s has enough capacity already, do nothing.
110 * If s does not have enough capacity, then allocate enough capacity,
111 *  copy the old string and the null terminator, free the old string,
112 *  and update all relevant metadata.
113 * Return s with the above changes.
114 */
115UTString* utstrrealloc(UTString* s, uint32_t new_capacity) {
116    assert(isOurs(s));
117    if(new_capacity <= s->capacity) {
118        return s;
119    }
120    s->capacity = new_capacity;
121    s->string = (char*)realloc(s->string, new_capacity + 5);
122    addCheck(s);
123    return s;
124}