001: /*
002: * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
003: * Copyright (C) 2007 - Javolution (http://javolution.org/)
004: * All rights reserved.
005: *
006: * Permission to use, copy, modify, and distribute this software is
007: * freely granted, provided that this notice is preserved.
008: */
009: package javolution.context;
010:
011: import javolution.lang.Configurable;
012:
013: /**
014: * <p> This class represents an allocator context; it defines the
015: * the allocation policy of {@link ObjectFactory} products.</p>
016: *
017: * <p> The {@link #DEFAULT default} implementation is an instance of
018: * {@link HeapContext} context.</p>
019: *
020: * <p> Specializations may allocate from local stacks ({@link StackContext}),
021: * specific memory areas (e.g. {@link ImmortalContext}), aging pools (where
022: * objects sufficiently old are recycled), switchable spaces (objects from
023: * a particular frame are recycled when buffers are swapped), etc.</p>
024: *
025: * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
026: * @version 5.2, August 19, 2007
027: */
028: public abstract class AllocatorContext extends Context {
029:
030: /**
031: * Holds the default allocator context instance.
032: */
033: private static volatile AllocatorContext _Default = new HeapContext();
034:
035: /**
036: * Holds the default implementation ({@link HeapContext} instance).
037: */
038: public static final Configurable/*<Class<? extends AllocatorContext>>*/
039: DEFAULT = new Configurable(_Default.getClass()) {
040: protected void notifyChange() {
041: _Default = (AllocatorContext) ObjectFactory.getInstance(
042: (Class) get()).object();
043: }
044: };
045:
046: /**
047: * Default constructor.
048: */
049: protected AllocatorContext() {
050: }
051:
052: /**
053: * Returns the current allocator context. If the current thread has
054: * not entered an allocator context (e.g. new thread) then
055: * {@link #getDefault()} is returned.
056: *
057: * @return the current allocator context.
058: */
059: public static/*AllocatorContext*/Context getCurrent() {
060: return Context.getCurrent().getAllocatorContext();
061: }
062:
063: /**
064: * Returns the default instance ({@link #DEFAULT} implementation).
065: *
066: * @return the default instance.
067: */
068: public static AllocatorContext getDefault() {
069: return AllocatorContext._Default;
070: }
071:
072: /**
073: * Returns the allocator for the specified factory in this context.
074: *
075: * @param factory the factory for which the allocator is returned.
076: * @return the allocator producing instances of the specified factory.
077: */
078: protected abstract Allocator getAllocator(ObjectFactory factory);
079:
080: /**
081: * Deactivates the {@link Allocator allocators} belonging to this context
082: * for the current thread. This method is typically called when an inner
083: * allocator context is entered by the current thread, when exiting an
084: * allocator context or when a concurrent executor has completed its task
085: * within this allocator context. Deactivated allocators have no
086: * {@link Allocator#user user} (<code>null</code>).
087: */
088: protected abstract void deactivate();
089:
090: /**
091: * <p> This class represents a {@link javolution.lang.Reference reference}
092: * allocated from the current {@link AllocatorContext}.
093: * The reachability level of this reference is the scope of the
094: * {@link AllocatorContext} in which it has been
095: * {@link #newInstance created}.</p>
096: *
097: * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
098: * @version 5.0, April 14, 2007
099: */
100: public static class Reference/*<T>*/implements
101: javolution.lang.Reference/*<T>*/{
102:
103: /**
104: * Holds the factory.
105: */
106: private static final ObjectFactory FACTORY = new ObjectFactory() {
107: protected Object create() {
108: return new Reference();
109: }
110:
111: protected void cleanup(Object obj) {
112: ((Reference) obj)._value = null;
113: }
114: };
115:
116: /**
117: * Holds the reference value.
118: */
119: private Object/*{T}*/_value;
120:
121: /**
122: * Default constructor.
123: */
124: public Reference() {
125: }
126:
127: /**
128: * Returns a new stack reference instance allocated on the current stack
129: * when executing in a {@link StackContext}.
130: *
131: * @return a reference object possibly recycled.
132: */
133: public static/*<T>*/Reference /*<T>*/newInstance() {
134: return (Reference) FACTORY.object();
135: }
136:
137: /**
138: * Returns the string representation of the current value of
139: * this reference.
140: *
141: * @return <code>String.valueOf(this.get())</code>
142: */
143: public String toString() {
144: return String.valueOf(_value);
145: }
146:
147: // Implements Reference interface.
148: public final Object/*{T}*/get() {
149: return _value;
150: }
151:
152: // Implements Reference interface.
153: public final void set(Object/*{T}*/value) {
154: _value = value;
155: }
156: }
157: }
|