vfs: check userland buffers before reading them.
[haiku.git] / headers / private / fs_shell / Stack.h
blob63b91d83c18d6cbc4ad3a77e42ed8f795995bf2a
1 /* Stack - a template stack class (plus some handy methods)
3 * Copyright 2001-2005, Axel Dörfler, axeld@pinc-software.de.
4 * This file may be used under the terms of the MIT License.
5 */
6 #ifndef _FSSH_STACK_H
7 #define _FSSH_STACK_H
10 #include "fssh_defs.h"
11 #include "fssh_errors.h"
14 namespace FSShell {
17 template<class T> class Stack {
18 public:
19 Stack()
21 fArray(NULL),
22 fUsed(0),
23 fMax(0)
27 ~Stack()
29 free(fArray);
32 bool IsEmpty() const
34 return fUsed == 0;
37 void MakeEmpty()
39 // could also free the memory
40 fUsed = 0;
43 fssh_status_t Push(T value)
45 if (fUsed >= fMax) {
46 fMax += 16;
47 T *newArray = (T *)realloc(fArray, fMax * sizeof(T));
48 if (newArray == NULL)
49 return FSSH_B_NO_MEMORY;
51 fArray = newArray;
53 fArray[fUsed++] = value;
54 return FSSH_B_OK;
57 bool Pop(T *value)
59 if (fUsed == 0)
60 return false;
62 *value = fArray[--fUsed];
63 return true;
66 T *Array()
68 return fArray;
71 int32_t CountItems() const
73 return fUsed;
76 private:
77 T *fArray;
78 int32_t fUsed;
79 int32_t fMax;
82 } // namespace FSShell
84 using FSShell::Stack;
87 #endif /* _FSSH_STACK_H */