/** * @return current alignment setting for this structure */ protected int getStructAlignment() { if (size == CALCULATE_SIZE) { // calculate size, but don't allocate memory calculateSize(true); } return structAlignment; }
/** Calculate the amount of native memory required for this structure. * May return {@link #CALCULATE_SIZE} if the size can not yet be * determined (usually due to fields in the derived class not yet * being initialized). * If the <code>force</code> parameter is <code>true</code> will throw * an {@link IllegalStateException} if the size can not be determined. * @param force whether to force size calculation * @return calculated size, or {@link #CALCULATE_SIZE} if the size can not * yet be determined. * @throws IllegalStateException an array field is not initialized or the * size can not be determined while <code>force</code> is <code>true</code>. * @throws IllegalArgumentException when an unsupported field type is * encountered */ protected int calculateSize(boolean force) { return calculateSize(force, false); }
private void allocateMemory(boolean avoidFFIType) { allocateMemory(calculateSize(true, avoidFFIType)); }
/** Provided for derived classes to indicate a different * size than the default. Returns whether the operation was successful. * Will leave memory untouched if it is non-null and not allocated * by this class. * @param size how much memory to allocate */ protected void allocateMemory(int size) { if (size == CALCULATE_SIZE) { // Analyze the struct, but don't worry if we can't yet do it size = calculateSize(false); } else if (size <= 0) { throw new IllegalArgumentException("Structure size must be greater than zero: " + size); } // May need to defer size calculation if derived class not fully // initialized if (size != CALCULATE_SIZE) { if (this.memory == null || this.memory instanceof AutoAllocated) { this.memory = autoAllocate(size); } this.size = size; } }
/** Ensure this memory has its size and layout calculated and its memory allocated. @param avoidFFIType used when computing FFI type information to avoid recursion */ private void ensureAllocated(boolean avoidFFIType) { if (memory == null) { allocateMemory(avoidFFIType); } else if (size == CALCULATE_SIZE) { this.size = calculateSize(true, avoidFFIType); if (!(this.memory instanceof AutoAllocated)) { // Ensure we've set bounds on the shared memory used try { this.memory = this.memory.share(0, this.size); } catch(IndexOutOfBoundsException e) { throw new IllegalArgumentException("Structure exceeds provided memory bounds", e); } } } }
size = calculateSize(false);
/** * @return current alignment setting for this structure */ protected int getStructAlignment() { if (size == CALCULATE_SIZE) { // calculate size, but don't allocate memory calculateSize(true); } return structAlignment; }
/** Calculate the amount of native memory required for this structure. * May return {@link #CALCULATE_SIZE} if the size can not yet be * determined (usually due to fields in the derived class not yet * being initialized). * If the <code>force</code> parameter is <code>true</code> will throw * an {@link IllegalStateException} if the size can not be determined. * @param force whether to force size calculation * @return calculated size, or {@link #CALCULATE_SIZE} if the size can not * yet be determined. * @throws IllegalStateException an array field is not initialized or the * size can not be determined while <code>force</code> is <code>true</code>. * @throws IllegalArgumentException when an unsupported field type is * encountered */ protected int calculateSize(boolean force) { return calculateSize(force, false); }
private void allocateMemory(boolean avoidFFIType) { allocateMemory(calculateSize(true, avoidFFIType)); }
/** Provided for derived classes to indicate a different * size than the default. Returns whether the operation was successful. * Will leave memory untouched if it is non-null and not allocated * by this class. * @param size how much memory to allocate */ protected void allocateMemory(int size) { if (size == CALCULATE_SIZE) { // Analyze the struct, but don't worry if we can't yet do it size = calculateSize(false); } else if (size <= 0) { throw new IllegalArgumentException("Structure size must be greater than zero: " + size); } // May need to defer size calculation if derived class not fully // initialized if (size != CALCULATE_SIZE) { if (this.memory == null || this.memory instanceof AutoAllocated) { this.memory = autoAllocate(size); } this.size = size; } }
/** Ensure this memory has its size and layout calculated and its memory allocated. @param avoidFFIType used when computing FFI type information to avoid recursion */ private void ensureAllocated(boolean avoidFFIType) { if (memory == null) { allocateMemory(avoidFFIType); } else if (size == CALCULATE_SIZE) { this.size = calculateSize(true, avoidFFIType); if (!(this.memory instanceof AutoAllocated)) { // Ensure we've set bounds on the shared memory used try { this.memory = this.memory.share(0, this.size); } catch(IndexOutOfBoundsException e) { throw new IllegalArgumentException("Structure exceeds provided memory bounds", e); } } } }
/** Adjust the size to be the size of the largest element, and ensure * all fields begin at offset zero. */ int calculateSize(boolean force) { int size = super.calculateSize(force); if (size != CALCULATE_SIZE) { int fsize = 0; for (Iterator i=fields().values().iterator();i.hasNext();) { StructField f = (StructField)i.next(); f.offset = 0; if (f.size > fsize) { fsize = f.size; biggestField = f; } } size = calculateAlignedSize(fsize); } return size; } /** All fields are considered the "first" element. */
size = calculateSize(false);