jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/
|
| 2 | /* Copyright (c) FIRST 2008. All Rights Reserved. */
|
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */
|
| 4 | /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
|
| 5 | /*----------------------------------------------------------------------------*/
|
| 6 |
|
| 7 | #ifndef RESOURCE_H_
|
| 8 | #define RESOURCE_H_
|
| 9 |
|
| 10 | #include "ErrorBase.h"
|
| 11 | #include "Synchronized.h"
|
| 12 | #include <vxWorks.h>
|
| 13 |
|
| 14 | /**
|
| 15 | * The Resource class is a convenient way to track allocated resources.
|
| 16 | * It tracks them as indicies in the range [0 .. elements - 1].
|
| 17 | * E.g. the library uses this to track hardware channel allocation.
|
| 18 | *
|
| 19 | * The Resource class does not allocate the hardware channels or other
|
| 20 | * resources; it just tracks which indices were marked in use by
|
| 21 | * Allocate and not yet freed by Free.
|
| 22 | */
|
| 23 | class Resource : public ErrorBase
|
| 24 | {
|
| 25 | public:
|
| 26 | virtual ~Resource();
|
| 27 | static void CreateResourceObject(Resource **r, UINT32 elements);
|
| 28 | UINT32 Allocate(const char *resourceDesc);
|
| 29 | UINT32 Allocate(UINT32 index, const char *resourceDesc);
|
| 30 | void Free(UINT32 index);
|
| 31 |
|
| 32 | private:
|
| 33 | explicit Resource(UINT32 size);
|
| 34 |
|
| 35 | bool *m_isAllocated;
|
| 36 | ReentrantSemaphore m_allocateLock;
|
| 37 | UINT32 m_size;
|
| 38 |
|
| 39 | static ReentrantSemaphore m_createLock;
|
| 40 |
|
| 41 | DISALLOW_COPY_AND_ASSIGN(Resource);
|
| 42 | };
|
| 43 |
|
| 44 | #endif
|