Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/gfx/2d/UserData.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef MOZILLA_GFX_USERDATA_H_
8
#define MOZILLA_GFX_USERDATA_H_
9
10
#include <stdlib.h>
11
#include "Types.h"
12
#include "mozilla/Assertions.h"
13
14
namespace mozilla {
15
namespace gfx {
16
17
struct UserDataKey {
18
  int unused;
19
};
20
21
/* this class is basically a clone of the user data concept from cairo */
22
class UserData
23
{
24
  typedef void (*destroyFunc)(void *data);
25
public:
26
0
  UserData() : count(0), entries(nullptr) {}
27
28
  /* Attaches untyped userData associated with key. destroy is called on destruction */
29
  void Add(UserDataKey *key, void *userData, destroyFunc destroy)
30
0
  {
31
0
    for (int i=0; i<count; i++) {
32
0
      if (key == entries[i].key) {
33
0
        if (entries[i].destroy) {
34
0
          entries[i].destroy(entries[i].userData);
35
0
        }
36
0
        entries[i].userData = userData;
37
0
        entries[i].destroy = destroy;
38
0
        return;
39
0
      }
40
0
    }
41
0
42
0
    // We could keep entries in a std::vector instead of managing it by hand
43
0
    // but that would propagate an stl dependency out which we'd rather not
44
0
    // do (see bug 666609). Plus, the entries array is expect to stay small
45
0
    // so doing a realloc everytime we add a new entry shouldn't be too costly
46
0
    entries = static_cast<Entry*>(realloc(entries, sizeof(Entry)*(count+1)));
47
0
48
0
    if (!entries) {
49
0
      MOZ_CRASH("GFX: UserData::Add");
50
0
    }
51
0
52
0
    entries[count].key      = key;
53
0
    entries[count].userData = userData;
54
0
    entries[count].destroy  = destroy;
55
0
56
0
    count++;
57
0
  }
58
59
  /* Remove and return user data associated with key, without destroying it */
60
  void* Remove(UserDataKey *key)
61
  {
62
    for (int i=0; i<count; i++) {
63
      if (key == entries[i].key) {
64
        void *userData = entries[i].userData;
65
        // decrement before looping so entries[i+1] doesn't read past the end:
66
        --count;
67
        for (;i<count; i++) {
68
          entries[i] = entries[i+1];
69
        }
70
        return userData;
71
      }
72
    }
73
    return nullptr;
74
  }
75
76
  /* Remove and destroy a given key */
77
  void RemoveAndDestroy(UserDataKey *key)
78
0
  {
79
0
    for (int i=0; i<count; i++) {
80
0
      if (key == entries[i].key) {
81
0
        if (entries[i].destroy) {
82
0
          entries[i].destroy(entries[i].userData);
83
0
        }
84
0
        // decrement before looping so entries[i+1] doesn't read past the end:
85
0
        --count;
86
0
        for (;i<count; i++) {
87
0
          entries[i] = entries[i+1];
88
0
        }
89
0
      }
90
0
    }
91
0
  }
92
93
  /* Retrives the userData for the associated key */
94
  void *Get(UserDataKey *key) const
95
0
  {
96
0
    for (int i=0; i<count; i++) {
97
0
      if (key == entries[i].key) {
98
0
        return entries[i].userData;
99
0
      }
100
0
    }
101
0
    return nullptr;
102
0
  }
103
104
  bool Has(UserDataKey *key)
105
  {
106
    for (int i=0; i<count; i++) {
107
      if (key == entries[i].key) {
108
        return true;
109
      }
110
    }
111
    return false;
112
  }
113
114
  void Destroy()
115
0
  {
116
0
    for (int i=0; i<count; i++) {
117
0
      if (entries[i].destroy) {
118
0
        entries[i].destroy(entries[i].userData);
119
0
      }
120
0
    }
121
0
    free(entries);
122
0
    entries = nullptr;
123
0
    count = 0;
124
0
  }
125
126
  ~UserData()
127
0
  {
128
0
    Destroy();
129
0
  }
130
131
private:
132
  struct Entry {
133
    const UserDataKey *key;
134
    void *userData;
135
    destroyFunc destroy;
136
  };
137
138
  int count;
139
  Entry *entries;
140
141
};
142
143
} // namespace gfx
144
} // namespace mozilla
145
146
#endif /* MOZILLA_GFX_USERDATA_H_ */