source: rtems-docs/ada_user/region_manager.rst @ 4783b0d

4.115
Last change on this file since 4783b0d was 4783b0d, checked in by Amar Takhar <amar@…>, on 01/17/16 at 16:37:28

Split document into seperate files by section.

  • Property mode set to 100644
File size: 19.5 KB
Line 
1Region Manager
2##############
3
4.. index:: regions
5
6Introduction
7============
8
9The region manager provides facilities to dynamically
10allocate memory in variable sized units.  The directives
11provided by the region manager are:
12
13- ``rtems.region_create`` - Create a region
14
15- ``rtems.region_ident`` - Get ID of a region
16
17- ``rtems.region_delete`` - Delete a region
18
19- ``rtems.region_extend`` - Add memory to a region
20
21- ``rtems.region_get_segment`` - Get segment from a region
22
23- ``rtems.region_return_segment`` - Return segment to a region
24
25- ``rtems.region_get_segment_size`` - Obtain size of a segment
26
27- ``rtems.region_resize_segment`` - Change size of a segment
28
29Background
30==========
31
32Region Manager Definitions
33--------------------------
34.. index:: region, definition
35.. index:: segment, definition
36
37A region makes up a physically contiguous memory
38space with user-defined boundaries from which variable-sized
39segments are dynamically allocated and deallocated.  A segment
40is a variable size section of memory which is allocated in
41multiples of a user-defined page size.  This page size is
42required to be a multiple of four greater than or equal to four.
43For example, if a request for a 350-byte segment is made in a
44region with 256-byte pages, then a 512-byte segment is allocated.
45
46Regions are organized as doubly linked chains of
47variable sized memory blocks.  Memory requests are allocated
48using a first-fit algorithm.  If available, the requester
49receives the number of bytes requested (rounded up to the next
50page size).  RTEMS requires some overhead from the region’s
51memory for each segment that is allocated.  Therefore, an
52application should only modify the memory of a segment that has
53been obtained from the region.  The application should NOT
54modify the memory outside of any obtained segments and within
55the region’s boundaries while the region is currently active in
56the system.
57
58Upon return to the region, the free block is
59coalesced with its neighbors (if free) on both sides to produce
60the largest possible unused block.
61
62Building an Attribute Set
63-------------------------
64.. index:: region attribute set, building
65
66In general, an attribute set is built by a bitwise OR
67of the desired attribute components.  The set of valid region
68attributes is provided in the following table:
69
70- ``RTEMS.FIFO`` - tasks wait by FIFO (default)
71
72- ``RTEMS.PRIORITY`` - tasks wait by priority
73
74Attribute values are specifically designed to be
75mutually exclusive, therefore bitwise OR and addition operations
76are equivalent as long as each attribute appears exactly once in
77the component list.  An attribute listed as a default is not
78required to appear in the attribute list, although it is a good
79programming practice to specify default attributes.  If all
80defaults are desired, the attribute``RTEMS.DEFAULT_ATTRIBUTES`` should be
81specified on this call.
82
83This example demonstrates the attribute_set parameter
84needed to create a region with the task priority waiting queue
85discipline.  The attribute_set parameter to the``rtems.region_create``
86directive should be ``RTEMS.PRIORITY``.
87
88Building an Option Set
89----------------------
90
91In general, an option is built by a bitwise OR of the
92desired option components.  The set of valid options for the``rtems.region_get_segment`` directive are
93listed in the following table:
94
95- ``RTEMS.WAIT`` - task will wait for segment (default)
96
97- ``RTEMS.NO_WAIT`` - task should not wait
98
99Option values are specifically designed to be
100mutually exclusive, therefore bitwise OR and addition operations
101are equivalent as long as each option appears exactly once in
102the component list.  An option listed as a default is not
103required to appear in the option list, although it is a good
104programming practice to specify default options.  If all
105defaults are desired, the option``RTEMS.DEFAULT_OPTIONS`` should be
106specified on this call.
107
108This example demonstrates the option parameter needed
109to poll for a segment.  The option parameter passed to the``rtems.region_get_segment`` directive should
110be ``RTEMS.NO_WAIT``.
111
112Operations
113==========
114
115Creating a Region
116-----------------
117
118The ``rtems.region_create`` directive creates a region with the
119user-defined name.  The user may select FIFO or task priority as
120the method for placing waiting tasks in the task wait queue.
121RTEMS allocates a Region Control Block (RNCB) from the RNCB free
122list to maintain the newly created region.  RTEMS also generates
123a unique region ID which is returned to the calling task.
124
125It is not possible to calculate the exact number of
126bytes available to the user since RTEMS requires overhead for
127each segment allocated.  For example, a region with one segment
128that is the size of the entire region has more available bytes
129than a region with two segments that collectively are the size
130of the entire region.  This is because the region with one
131segment requires only the overhead for one segment, while the
132other region requires the overhead for two segments.
133
134Due to automatic coalescing, the number of segments
135in the region dynamically changes.  Therefore, the total
136overhead required by RTEMS dynamically changes.
137
138Obtaining Region IDs
139--------------------
140
141When a region is created, RTEMS generates a unique
142region ID and assigns it to the created region until it is
143deleted.  The region ID may be obtained by either of two
144methods.  First, as the result of an invocation of the``rtems.region_create`` directive,
145the region ID is stored in a user
146provided location.  Second, the region ID may be obtained later
147using the ``rtems.region_ident`` directive.
148The region ID is used by other region manager directives to
149access this region.
150
151Adding Memory to a Region
152-------------------------
153
154The ``rtems.region_extend`` directive may be used to add memory
155to an existing region.  The caller specifies the size in bytes
156and starting address of the memory being added.
157
158NOTE:  Please see the release notes or RTEMS source
159code for information regarding restrictions on the location of
160the memory being added in relation to memory already in the
161region.
162
163Acquiring a Segment
164-------------------
165
166The ``rtems.region_get_segment`` directive attempts to acquire
167a segment from a specified region.  If the region has enough
168available free memory, then a segment is returned successfully
169to the caller.  When the segment cannot be allocated, one of the
170following situations applies:
171
172- By default, the calling task will wait forever to acquire the segment.
173
174- Specifying the ``RTEMS.NO_WAIT`` option forces
175  an immediate return with an error status code.
176
177- Specifying a timeout limits the interval the task will
178  wait before returning with an error status code.
179
180If the task waits for the segment, then it is placed
181in the region’s task wait queue in either FIFO or task priority
182order.  All tasks waiting on a region are returned an error when
183the message queue is deleted.
184
185Releasing a Segment
186-------------------
187
188When a segment is returned to a region by the``rtems.region_return_segment`` directive, it is merged with its
189unallocated neighbors to form the largest possible segment.  The
190first task on the wait queue is examined to determine if its
191segment request can now be satisfied.  If so, it is given a
192segment and unblocked.  This process is repeated until the first
193task’s segment request cannot be satisfied.
194
195Obtaining the Size of a Segment
196-------------------------------
197
198The ``rtems.region_get_segment_size`` directive returns the
199size in bytes of the specified segment.  The size returned
200includes any "extra" memory included in the segment because of
201rounding up to a page size boundary.
202
203Changing the Size of a Segment
204------------------------------
205
206The ``rtems.region_resize_segment`` directive is used
207to change the size in bytes of the specified segment.  The size may be
208increased or decreased.  When increasing the size of a segment, it is
209possible that the request cannot be satisfied.  This directive provides
210functionality similar to the ``realloc()`` function in the Standard
211C Library.
212
213Deleting a Region
214-----------------
215
216A region can be removed from the system and returned
217to RTEMS with the ``rtems.region_delete``
218directive.  When a region is
219deleted, its control block is returned to the RNCB free list.  A
220region with segments still allocated is not allowed to be
221deleted.  Any task attempting to do so will be returned an
222error.  As a result of this directive, all tasks blocked waiting
223to obtain a segment from the region will be readied and returned
224a status code which indicates that the region was deleted.
225
226Directives
227==========
228
229This section details the region manager’s directives.
230A subsection is dedicated to each of this manager’s directives
231and describes the calling sequence, related constants, usage,
232and status codes.
233
234REGION_CREATE - Create a region
235-------------------------------
236.. index:: create a region
237
238**CALLING SEQUENCE:**
239
240.. code:: c
241
242    procedure Region_Create (
243    Name             : in     RTEMS.Name;
244    Starting_Address : in     RTEMS.Address;
245    Length           : in     RTEMS.Unsigned32;
246    Page_Size        : in     RTEMS.Unsigned32;
247    Attribute_Set    : in     RTEMS.Attribute;
248    ID               :    out RTEMS.ID;
249    Result           :    out RTEMS.Status_Codes
250    );
251
252**DIRECTIVE STATUS CODES:**
253
254``RTEMS.SUCCESSFUL`` - region created successfully
255``RTEMS.INVALID_NAME`` - invalid region name
256``RTEMS.INVALID_ADDRESS`` - ``id`` is NULL
257``RTEMS.INVALID_ADDRESS`` - ``starting_address`` is NULL
258``RTEMS.INVALID_ADDRESS`` - address not on four byte boundary
259``RTEMS.TOO_MANY`` - too many regions created
260``RTEMS.INVALID_SIZE`` - invalid page size
261
262**DESCRIPTION:**
263
264This directive creates a region from a physically
265contiguous memory space which starts at starting_address and is
266length bytes long.  Segments allocated from the region will be a
267multiple of page_size bytes in length.  The assigned region id
268is returned in id.  This region id is used as an argument to
269other region related directives to access the region.
270
271For control and maintenance of the region, RTEMS
272allocates and initializes an RNCB from the RNCB free pool.  Thus
273memory from the region is not used to store the RNCB.  However,
274some overhead within the region is required by RTEMS each time a
275segment is constructed in the region.
276
277Specifying ``RTEMS.PRIORITY`` in attribute_set causes tasks
278waiting for a segment to be serviced according to task priority.
279Specifying ``RTEMS.FIFO`` in attribute_set or selecting``RTEMS.DEFAULT_ATTRIBUTES`` will cause waiting tasks to
280be serviced in First In-First Out order.
281
282The ``starting_address`` parameter must be aligned on a
283four byte boundary.  The ``page_size`` parameter must be a multiple
284of four greater than or equal to eight.
285
286**NOTES:**
287
288This directive will not cause the calling task to be
289preempted.
290
291The following region attribute constants are defined
292by RTEMS:
293
294- ``RTEMS.FIFO`` - tasks wait by FIFO (default)
295
296- ``RTEMS.PRIORITY`` - tasks wait by priority
297
298REGION_IDENT - Get ID of a region
299---------------------------------
300.. index:: get ID of a region
301.. index:: obtain ID of a region
302
303**CALLING SEQUENCE:**
304
305.. code:: c
306
307    procedure Region_Ident (
308    Name   : in     RTEMS.Name;
309    ID     :    out RTEMS.ID;
310    Result :    out RTEMS.Status_Codes
311    );
312
313**DIRECTIVE STATUS CODES:**
314
315``RTEMS.SUCCESSFUL`` - region identified successfully
316``RTEMS.INVALID_ADDRESS`` - ``id`` is NULL
317``RTEMS.INVALID_NAME`` - region name not found
318
319**DESCRIPTION:**
320
321This directive obtains the region id associated with
322the region name to be acquired.  If the region name is not
323unique, then the region id will match one of the regions with
324that name.  However, this region id is not guaranteed to
325correspond to the desired region.  The region id is used to
326access this region in other region manager directives.
327
328**NOTES:**
329
330This directive will not cause the running task to be preempted.
331
332REGION_DELETE - Delete a region
333-------------------------------
334.. index:: delete a region
335
336**CALLING SEQUENCE:**
337
338.. code:: c
339
340    procedure Region_Delete (
341    ID     : in     RTEMS.ID;
342    Result :    out RTEMS.Status_Codes
343    );
344
345**DIRECTIVE STATUS CODES:**
346
347``RTEMS.SUCCESSFUL`` - region deleted successfully
348``RTEMS.INVALID_ID`` - invalid region id
349``RTEMS.RESOURCE_IN_USE`` - segments still in use
350
351**DESCRIPTION:**
352
353This directive deletes the region specified by id.
354The region cannot be deleted if any of its segments are still
355allocated.  The RNCB for the deleted region is reclaimed by
356RTEMS.
357
358**NOTES:**
359
360This directive will not cause the calling task to be preempted.
361
362The calling task does not have to be the task that
363created the region.  Any local task that knows the region id can
364delete the region.
365
366REGION_EXTEND - Add memory to a region
367--------------------------------------
368.. index:: add memory to a region
369.. index:: region, add memory
370
371**CALLING SEQUENCE:**
372
373.. code:: c
374
375    procedure Region_Extend (
376    ID               : in     RTEMS.ID;
377    Starting_Address : in     RTEMS.Address;
378    Length           : in     RTEMS.Unsigned32;
379    Result           :    out RTEMS.Status_Codes
380    );
381
382**DIRECTIVE STATUS CODES:**
383
384``RTEMS.SUCCESSFUL`` - region extended successfully
385``RTEMS.INVALID_ADDRESS`` - ``starting_address`` is NULL
386``RTEMS.INVALID_ID`` - invalid region id
387``RTEMS.INVALID_ADDRESS`` - invalid address of area to add
388
389**DESCRIPTION:**
390
391This directive adds the memory which starts at
392starting_address for length bytes to the region specified by id.
393
394**NOTES:**
395
396This directive will not cause the calling task to be preempted.
397
398The calling task does not have to be the task that
399created the region.  Any local task that knows the region id can
400extend the region.
401
402REGION_GET_SEGMENT - Get segment from a region
403----------------------------------------------
404.. index:: get segment from region
405
406**CALLING SEQUENCE:**
407
408.. code:: c
409
410    procedure Region_Get_Segment (
411    ID         : in     RTEMS.ID;
412    Size       : in     RTEMS.Unsigned32;
413    Option_Set : in     RTEMS.Option;
414    Timeout    : in     RTEMS.Interval;
415    Segment    :    out RTEMS.Address;
416    Result     :    out RTEMS.Status_Codes
417    );
418
419**DIRECTIVE STATUS CODES:**
420
421``RTEMS.SUCCESSFUL`` - segment obtained successfully
422``RTEMS.INVALID_ADDRESS`` - ``segment`` is NULL
423``RTEMS.INVALID_ID`` - invalid region id
424``RTEMS.INVALID_SIZE`` - request is for zero bytes or exceeds
425the size of maximum segment which is possible for this region
426``RTEMS.UNSATISFIED`` - segment of requested size not available
427``RTEMS.TIMEOUT`` - timed out waiting for segment
428``RTEMS.OBJECT_WAS_DELETED`` - region deleted while waiting
429
430**DESCRIPTION:**
431
432This directive obtains a variable size segment from
433the region specified by id.  The address of the allocated
434segment is returned in segment.  The ``RTEMS.WAIT``
435and ``RTEMS.NO_WAIT`` components
436of the options parameter are used to specify whether the calling
437tasks wish to wait for a segment to become available or return
438immediately if no segment is available.  For either option, if a
439sufficiently sized segment is available, then the segment is
440successfully acquired by returning immediately with  the``RTEMS.SUCCESSFUL`` status code.
441
442If the calling task chooses to return immediately and
443a segment large enough is not available, then an error code
444indicating this fact is returned.  If the calling task chooses
445to wait for the segment and a segment large enough is not
446available, then the calling task is placed on the region’s
447segment wait queue and blocked.  If the region was created with
448the ``RTEMS.PRIORITY`` option, then the calling
449task is inserted into the
450wait queue according to its priority.  However, if the region
451was created with the ``RTEMS.FIFO`` option, then the calling
452task is placed at the rear of the wait queue.
453
454The timeout parameter specifies the maximum interval
455that a task is willing to wait to obtain a segment.  If timeout
456is set to ``RTEMS.NO_TIMEOUT``, then the
457calling task will wait forever.
458
459**NOTES:**
460
461The actual length of the allocated segment may be
462larger than the requested size because a segment size is always
463a multiple of the region’s page size.
464
465The following segment acquisition option constants
466are defined by RTEMS:
467
468- ``RTEMS.WAIT`` - task will wait for segment (default)
469
470- ``RTEMS.NO_WAIT`` - task should not wait
471
472A clock tick is required to support the timeout functionality of
473this directive.
474
475REGION_RETURN_SEGMENT - Return segment to a region
476--------------------------------------------------
477.. index:: return segment to region
478
479**CALLING SEQUENCE:**
480
481.. code:: c
482
483    procedure Region_Return_Segment (
484    ID      : in     RTEMS.ID;
485    Segment : in     RTEMS.Address;
486    Result  :    out RTEMS.Status_Codes
487    );
488
489**DIRECTIVE STATUS CODES:**
490
491``RTEMS.SUCCESSFUL`` - segment returned successfully
492``RTEMS.INVALID_ADDRESS`` - ``segment`` is NULL
493``RTEMS.INVALID_ID`` - invalid region id
494``RTEMS.INVALID_ADDRESS`` - segment address not in region
495
496**DESCRIPTION:**
497
498This directive returns the segment specified by
499segment to the region specified by id.  The returned segment is
500merged with its neighbors to form the largest possible segment.
501The first task on the wait queue is examined to determine if its
502segment request can now be satisfied.  If so, it is given a
503segment and unblocked.  This process is repeated until the first
504task’s segment request cannot be satisfied.
505
506**NOTES:**
507
508This directive will cause the calling task to be
509preempted if one or more local tasks are waiting for a segment
510and the following conditions exist:
511
512- a waiting task has a higher priority than the calling task
513
514- the size of the segment required by the waiting task
515  is less than or equal to the size of the segment returned.
516
517REGION_GET_SEGMENT_SIZE - Obtain size of a segment
518--------------------------------------------------
519.. index:: get size of segment
520
521**CALLING SEQUENCE:**
522
523.. code:: c
524
525    procedure Region_Get_Segment_Size (
526    ID         : in     RTEMS.ID;
527    Segment    : in     RTEMS.Address;
528    Size       :    out RTEMS.Unsigned32;
529    Result     :    out RTEMS.Status_Codes
530    );
531
532**DIRECTIVE STATUS CODES:**
533
534``RTEMS.SUCCESSFUL`` - segment obtained successfully
535``RTEMS.INVALID_ADDRESS`` - ``segment`` is NULL
536``RTEMS.INVALID_ADDRESS`` - ``size`` is NULL
537``RTEMS.INVALID_ID`` - invalid region id
538``RTEMS.INVALID_ADDRESS`` - segment address not in region
539
540**DESCRIPTION:**
541
542This directive obtains the size in bytes of the specified segment.
543
544**NOTES:**
545
546The actual length of the allocated segment may be
547larger than the requested size because a segment size is always
548a multiple of the region’s page size.
549
550REGION_RESIZE_SEGMENT - Change size of a segment
551------------------------------------------------
552.. index:: resize segment
553
554**CALLING SEQUENCE:**
555
556.. code:: c
557
558    procedure Region_Resize_Segment (
559    ID         : in     RTEMS.ID;
560    Segment    : in     RTEMS.Address;
561    Size       : in     RTEMS.Unsigned32;
562    Old_Size   :    out RTEMS.Unsigned32;
563    Result     :    out RTEMS.Status_Codes
564    );
565
566**DIRECTIVE STATUS CODES:**
567
568``RTEMS.SUCCESSFUL`` - segment obtained successfully
569``RTEMS.INVALID_ADDRESS`` - ``segment`` is NULL
570``RTEMS.INVALID_ADDRESS`` - ``old_size`` is NULL
571``RTEMS.INVALID_ID`` - invalid region id
572``RTEMS.INVALID_ADDRESS`` - segment address not in region``RTEMS.UNSATISFIED`` - unable to make segment larger
573
574**DESCRIPTION:**
575
576This directive is used to increase or decrease the size of
577a segment.  When increasing the size of a segment, it
578is possible that there is not memory available contiguous
579to the segment.  In this case, the request is unsatisfied.
580
581**NOTES:**
582
583If an attempt to increase the size of a segment fails, then
584the application may want to allocate a new segment of the desired
585size, copy the contents of the original segment to the new, larger
586segment and then return the original segment.
587
588.. COMMENT: COPYRIGHT (c) 1988-2002.
589
590.. COMMENT: On-Line Applications Research Corporation (OAR).
591
592.. COMMENT: All rights reserved.
593
Note: See TracBrowser for help on using the repository browser.