private int getMaxElementsIgnoreExisting(Program program, Address addr, int elementSize) {

		// can't go past the end of a block to start with
		AddressSetView asv = program.getMemory();
		AddressRange range = asv.getRangeContaining(addr);
		if (range == null) {
			return 0;
		}
		Address maxAddress = range.getMaxAddress();

		Instruction instructionAfter = program.getListing().getInstructionAfter(addr);
		if (instructionAfter != null && maxAddress.compareTo(instructionAfter.getAddress()) > 0) {
			maxAddress = instructionAfter.getAddress().previous();
		}

		int length = (int) maxAddress.subtract(addr) + 1;
		if (length < 0) {
			return 0;
		}
		return (length / elementSize);
	}
Get the maximum number of elements that will fit.  This means the undefined
bytes available up to the next instruction or end of defined memory.

@return the maximum number of elements for an array of the given data type
Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/data/CreateArrayAction.java